C语言题如图(需用switch语句)
展开全部
#include<stdio.h>
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所有梯度的金额对比就能得到输入金额对应的折扣率。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询