用c语言实现一件衣服原价499,买一件打八折,2件7折,3件打五折,4件及以上打四折,从键盘输入件数,输出应付金额(结果保留2位小数)和实际消费金额(结果保留整数)
时间: 2023-07-11 16:24:14 浏览: 52
好的,以下是实现该功能的C语言代码:
```c
#include <stdio.h>
int main() {
int num; // 件数
float price, total; // 单价、应付金额
int actual; // 实际消费金额
printf("请输入购买件数:");
scanf("%d", &num);
if (num == 1) {
price = 499 * 0.8;
} else if (num == 2) {
price = 499 * 0.7 * num;
} else if (num == 3) {
price = 499 * 0.5 * num;
} else {
price = 499 * 0.4 * num;
}
total = price;
actual = (int)price;
printf("应付金额为:%.2f元\n", total);
printf("实际消费金额为:%d元\n", actual);
return 0;
}
```
代码解释:
1. 首先定义了需要用到的变量,包括件数、单价、应付金额、实际消费金额。
2. 然后通过从键盘输入件数,根据不同的条件计算出应付金额和实际消费金额。这里使用了if语句来判断件数的不同情况。
3. 最后输出应付金额和实际消费金额,其中应付金额保留两位小数,实际消费金额取整数部分。
希望能对您有所帮助!