int main() {
int arrAmount[] = { 1000, 500, 200, 0 }
// 每个梯度对应的折扣
double arrDiscount[] = { 0.7, 0.8, 0.9, 1.0 }
int index = -1, i = 0
double discount = 1.0, realAmount = 0
printf("请输入金额:")
int amount
scanf("%d", &amount)
if (amount < 0) {
printf("输入金额必须为非负数")
return 1
}
// 查找当前输入金额所对应的折扣率
for (i = 0 i < 4 i++) {
if (amount >= arrAmount[i]) {
index = i
break
}
}
// 当前金额对应的折扣率计算
switch(index) {
case 0:
discount = arrDiscount[0]
realAmount = amount * arrDiscount[0]
break
case 1:
discount = arrDiscount[1]
realAmount = amount * arrDiscount[1]
break
case 2:
discount = arrDiscount[2]
realAmount = amount * arrDiscount[2]
break
case 3:
discount = arrDiscount[3]
realAmount = amount * arrDiscount[3]
break
default:
return 1
}
printf("折扣率:%.2lf, 实际金额:%.2lf", discount, realAmount)
return 0
}
这道题真正的难点在于根据金额查找到对应的折扣率,通过从1000到0所有梯度的金额对比就能得到输入金额对应的折扣率。
#include <stdio.h>int main()
{
float cost,percent,c
printf("请输入商品的原价(单位:元)");
scanf("%f",&cost)/*第一空*/
printf("请输入折扣率:")
scanf("%f",&percent)
c=cost*percent
printf("实际售价为:%0.2f ",c)
}
#include <stdio.h>void main(){
float p,d
scanf("%f",&p)
if(p<100) d=0
else if(p<200) d=5
else if(p<500) d=10
else if(p<1000) d=15
else d=20
printf("折扣率:%.f%%,实付金额:%.2f",d,p-p*d/100)
}
运行示例: