小白求助,pintia上的一道题
#include <stdio.h>int main()
{
int i;
int bonus,bon1,bon2,bon4,bon6,bon10;
int branch;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
scanf("%d",&i);
branch=i/100000;
if (branch>10)branch=10;
else if (branch<0)branch=-1;
switch(branch)
{case -1:bonus=0;break;
case 0:bonus=i*0.1;break;
case 1:bonus=bon1+(i-100000)*0.075;break;
case 2:
case 3: bonus=bon2+(i-200000)*0.05;break;
case 4:
case 5: bonus=bon4+(i-400000)*0.03;break;
case 6:
case 7:
case 8:
case 9: bonus=bon6+(i-600000)*0.015;break;
case 10: bonus=bon10+(i-1000000)*0.01;
}
printf("prize=%d\n",bonus);
return 0;
}
改了好几回了,总提示部分答案错误,求大神改正。 既然是做浮点运算,为什么要用整形变量? xieglt 发表于 2020-10-27 08:45
既然是做浮点运算,为什么要用整形变量?
帮忙改一下呗,我自己改double,float总是编译错误 本帖最后由 xieglt 于 2020-10-27 09:37 编辑
936241151 发表于 2020-10-27 09:03
帮忙改一下呗,我自己改double,float总是编译错误
#include <stdio.h>
int main()
{
float i;
float bonus,bon1,bon2,bon4,bon6,bon10;
int branch;
bon1=100000*0.1f;
bon2=bon1+100000*0.075f;
bon4=bon2+200000*0.05f;
bon6=bon4+200000*0.03f;
bon10=bon6+400000*0.015f;
scanf("%f",&i);
branch = (int) (i/100000);
if(i < 0)
branch = -1;
else if (branch>10)
branch=10;
switch(branch)
{case -1: bonus=0;break;
case 0: bonus=i*0.1f;break;
case 1: bonus=bon1+(i-100000)*0.075f;break;
case 2:
case 3: bonus=bon2+(i-200000)*0.05f;break;
case 4:
case 5: bonus=bon4+(i-400000)*0.03f;break;
case 6:
case 7:
case 8:
case 9: bonus=bon6+(i-600000)*0.015f;break;
case 10: bonus=bon10+(i-1000000)*0.01f;
}
printf("prize=%f\n",bonus);
return 0;
} xieglt 发表于 2020-10-27 09:14
好像解决不了输入负数时输出全为0的情况,输入大于-100000的负数他还会去按case 0去算 936241151 发表于 2020-10-27 09:29
好像解决不了输入负数时输出全为0的情况,输入大于-100000的负数他还会去按case 0去算
修改了一下代码,你再看看 xieglt 发表于 2020-10-27 09:14
欧克啦 mua
页:
[1]