看看吧 这道题 switch 语句
企业发放的奖金根据利润提成。利润低于或等于 100000 元时,奖金可提 10%;利润高于 100000 元,低于 200000 元时,低于 100000 元的部分按 10%提成,高于 100000 元的部
分,可提成 7.5%;200000 到 400000 之间时,高于 200000 元的部分,可提成 5%;400000
到 600000 之间时高于 400000 元的部分,可提成 3%;600000 到 1000000 之间时,高于 600000
元的部分,可提成 1.5%,高于 1000000 元时,超过 1000000 元的部分按 1%提成,从键盘输
入当月利润,求应发放奖金总数。请应用 switch 语句编写算法实现。
下面代码是我自己打的 还没完 不会写了
#include <stdio.h>
int main()
{
int num,prize;
scanf("%d",num);
switch(num/100000)
{
case 0:prize=num*0.1;break;
case 1:prize=100000*0.1+(num-100000)*0.075;break;
case 2:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05;break;
case 3:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05;break;
case 4:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03;break;
case 5:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03;break;
case 6:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 7:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 8:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 9:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 10:prize=
default:prize=
}
}
我不知道 负数还有超过1000000的怎么写了 #include <stdio.h>
int main()
{
int num,prize;
scanf("%d",num);
switch(num/100000)
{
case 0:prize=num*0.1;break;
case 1:prize=100000*0.1+(num-100000)*0.075;break;
case 2:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05;break;
case 3:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05;break;
case 4:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03;break;
case 5:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03;break;
case 6:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 7:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 8:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 9:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 10:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015+(num-1000000)*0.01;break;
default:break;
}
}
应该是这样吧{:10_312:} 小甲鱼的铁粉 发表于 2020-10-26 20:30
应该是这样吧
不对吧 比如说两百万 除1000000就没有对应的case 题上示例负数输出是0 癞蛤蟆丶ccc 发表于 2020-10-26 20:33
不对吧 比如说两百万 除1000000就没有对应的case 题上示例负数输出是0
我明白了 #include <stdio.h>
int main()
{
int num,prize;
scanf("%d",num);
if(num <0)
{
printf("0");
return 0;
}
switch(num/100000)
{
case 0:prize=num*0.1;break;
case 1:prize=100000*0.1+(num-100000)*0.075;break;
case 2:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05;break;
case 3:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05;break;
case 4:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03;break;
case 5:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03;break;
case 6:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 7:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 8:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
case 9:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015;break;
default:prize=100000*0.1+(num-100000)*0.075+(num-200000)*0.05+(num-400000)*0.03+(num-600000)*0.015+(num-1000000)*0.01;break;
}
return 0;
}
小甲鱼的铁粉 发表于 2020-10-26 20:38
好像对哦 我中间式子错了 我等会改
癞蛤蟆丶ccc 发表于 2020-10-26 20:40
好像对哦 我中间式子错了 我等会改
如果问题解决,不要忘记最佳答案哦{:10_297:} 小甲鱼的铁粉 发表于 2020-10-26 20:42
如果问题解决,不要忘记最佳答案哦
嗯 应该可以这样写
int main(int argc, char const *argv[])
{
int num,prize;
scanf("%d",num);
prize = 0;
switch(num/100000) {
case 10:
prize += (num - 1000000) * 0.01;// 1%
num = 1000000;
case 9:
case 8:
case 7:
case 6:
prize += (num - 600000) * 0.015;// 1.5%
num = 600000;
case 5:
case 4:
prize += (num - 400000) * 0.03;// 3%
num = 400000;
case 3:
case 2:
prize += (num - 200000) * 0.05;// 5%
num = 200000;
case 1:
prize += (num - 100000) * 0.075;// 7.5%
num = 100000;
default:
prize += num * 0.1; // 10%
break;
}
return 0;
} 小甲鱼的铁粉 发表于 2020-10-26 20:38
细致 小甲鱼的铁粉 发表于 2020-10-26 20:42
如果问题解决,不要忘记最佳答案哦
不对 .if<0后 又去switch循环里面去了 default 小甲鱼的铁粉 发表于 2020-10-26 20:42
如果问题解决,不要忘记最佳答案哦
if写到下面就好了 {:10_254:}
页:
[1]