小明过年挣了些压岁钱,他打算把这些钱存到银行。银行存款年利率如下,存的时间越长,每年的利率越高。小明可以按不同的年限存款,请帮小明计算到期后的利息和本金。
1年期3%;
2年期3.3%;
3年期3.8%;
4年期4.0%;
5年及以上4.2%。
输入描述
输入两个数,第一个是存款本金,第二个存款年数。
输出描述
输出到期后的利息和总金额,保留两位小数,具体格式见示例输出。
用例输入 1
100.5 1
用例输出 1
interest=3.02, total=103.52
我的代码如下
#include
int main(){
float b;int n,i;
float interest,total;
scanf("%f %d",&b,&n);
if(n==1) interest=b*0.03;
else if(n==2) interest=b*2*0.033;
else if(n==3) interest=b*3*0.038;
else if(n==4) interest=b*4*0.04;
else if(n>=5) interest=b*n*0.042;
i=int((interest+0.005)*100);
interest=float(i);interest/=100;
total=b+interest;
printf("interest=%.2f, total=%.2f\n",interest,total);
return 0;
}
望采纳
#include<stdio.h>
int main(){
float b;int n,i;
float interest,total;
scanf("%f %d",&b,&n);
if(n==1) interest=b*0.03;
else if(n==2) interest=b*2*0.033;
else if(n==3) interest=b*3*0.038;
else if(n==4) interest=b*4*0.04;
else if(n>=5) interest=b*n*0.042;
i=int((interest+0.005)*100);
interest=float(i);interest/=100;
total=b+interest;
printf("interest=%.2f, total=%.2f\n",interest,total);
return 0;
}
运行结果如下:
interest=3.02, total=103.52
而且你的代码中使用了 int 和 float 类型的转换函数,但是这些函数不是 C 语言中的标准函数,可能无法在所有编译器中运行。
# 输入小明的存款本金和存款年数
principal = float(input("请输入存款本金:"))
years = int(input("请输入存款年数:"))
# 根据存款年数计算利率
if years == 1:
rate = 0.03
elif years == 2:
rate = 0.033
elif years == 3:
rate = 0.038
elif years == 4:
rate = 0.04
else:
rate = 0.042
# 计算利息和总金额
interest = principal * rate * years
total = principal + interest
# 输出结果
print(f"interest={interest:.2f}, total={total:.2f}")