1. Question & Analysis
- question: 企业发bonus是根据profit的多少给提成的;1. profit <= 10w,提成10%;2. 10w < profit < 20w, <=10w部分10%,>10w部分7.5%;3. 20~40w, >20w部分,提成5%;4. 40~60w,>40w的部分,提成3%; 5. 60~100w,>60的部分,提成1.5%; 6. >100w的部分,提成1%;
- analysis: 一大堆分层级的条件,想到的应该是 if -- else if --else 判断语句来执行;
2. Code Modules
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include <stdio.h>
int main() {
double profit = 0.0;
double bonus = 0.0;
printf("输入利润总额(万元)>:");
scanf("%lf", &profit);
if (profit < 10) {
bonus = 0.1 * profit;
}
else if (profit < 20) {
bonus = 0.1 * 10 + 0.075 * (profit - 10);
}
else if (profit < 40) {
bonus = 0.1 * 10 + 0.075 * 10 +0.05 * (profit - 20);
}
else if (profit < 60) {
bonus = 0.1 * 10 + 0.075 * 10 + 0.05 * 20 + 0.03 * (profit - 40);
}
else if (profit < 100) {
bonus = 0.1 * 10 + 0.075 * 10 + 0.05 * 20 + 0.03 * 20 + 0.015 * (profit - 60);
}
else {
bonus = 0.1 * 10 + 0.075 * 10 + 0.05 * 20 + 0.03 * 20 + 0.015 * 40 + 0.01 * (profit - 100);
}
printf("你的奖金为%.3lf万元\n", bonus);
return 0;
}
3. Summary
这里需要注意的点就是对profit和bonus的定义问题, 因为后面的判断条件中,提成是百分数,也就是浮点数,所以用double定义会稳妥一点,否则bonus会出现溢出的问题。像我这样的小白需要注意,我发帖更多的也是提醒我自己,哈哈哈