2个回答

`````` 用if语句
#include <stdio.h>
int main()
{
int i;
double bonus,bon1,bon2,bon4,bon6,bon10;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+100000*0.05;
bon6=bon4+100000*0.03;
bon10=bon6+400000*0.015;
printf("请输入利润i:");
scanf("%d",&i);
if (i<=100000)
bonus=i*0.1;
else if (i<=200000)
bonus=bon1+(i-100000)*0.075;
else if (i<=400000)
bonus=bon2+(i-200000)*0.05;
else if (i<=600000)
bonus=bon4+(i-400000)*0.03;
else if (i<=1000000)
bonus=bon6+(i-600000)*0.015;
else
bonus=bon10+(i-1000000)*0.01;
printf("奖金是: %10.2f\n",bonus);
return 0;
}

#include <stdio.h>
int main()
{
int i;
double  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;
printf("请输入利润i:");
scanf("%d",&i);
branch=i/100000;
if (branch>10)  branch=10;
switch(branch)
{  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("奖金是 %10.2f\n",bonus);
return 0;
}

``````

`````` #include<stdio.h>
double if_else(double x) {
double y=0;
if (x <= 10)
y = 0.1*x;
else if (x <= 20)
y = 1 + 0.075*(x - 10);
else if (x <= 40)
y = 1.75 + 0.05*(x - 20);
else if (x <= 60)
y = 2.75 + 0.03*(x - 40);
else
y = 3.95 + 0.01*(x - 60);
return y;
}
double _switch(double x) {
double y = 0;
switch ((int)x/10) {  //以十位为开关
case 0:
y = 0.1*x;
break;
case 1:
case 2:
y = 1 + 0.075*(x - 10);
break;
case 3:
case 4:
y = 1.75 + 0.05*(x - 20);
break;
case 5:
case 6:
y = 2.75 + 0.03*(x - 40);
break;
default:
y = 3.95 + 0.01*(x - 60);
break;
}
return y;
}
int main() {
double I;
printf("Enter the I :");
scanf("%lf", &I);
printf("if_else : %lf\n", if_else(I));
printf("_switch : %lf\n", _switch(I));
return 0;
}
``````