这个程序执行效率太低,每一个奖金都得重复计算多次,可以改进。输入格式的错误,这个不算讨论的范畴,可能是作者笔误。
以下是引用chihuyu在2012-2-13 20:43:37的发言:
#include"stdio.h"
#include"math.h"
void main()
{
double profit,bonus;
scanf("%f",&profit);
if(profit<=0)
{
printf("今年没有奖金!\n");
}
else
{
if(profit<=10)
{
bonus=profit*10.0/100;
}
else if(profit<=20)
{
bonus=10*10.0/100+(profit-10)*7.5/100;
}
else if(profit<=40)
{
bonus=10*10.0/100+10*7.5/100+(profit-20)*5.0/100;
}
else if(profit<=60)
{
bonus=10*10.0/100+10*7.5/100+20*5.0/100+(profit-40)*3.0/100;
}
else if(profit<=100)
{
bonus=10*10.0/100+10*7.5/100+20*5.0/100+20*3.0/100+(profit-60)*1.5/100;
}
else
{
bonus=10*10.0/100+10*7.5/100+20*5.0/100+20*3.0/100+40*1.5/100+(profit-100)*1.0/100;
}
printf("今年的奖金是%g\n",bonus);
}
}
#include"stdio.h"
#include"math.h"
void main()
{
double profit,bonus;
scanf("%f",&profit);
if(profit<=0)
{
printf("今年没有奖金!\n");
}
else
{
if(profit<=10)
{
bonus=profit*10.0/100;
}
else if(profit<=20)
{
bonus=10*10.0/100+(profit-10)*7.5/100;
}
else if(profit<=40)
{
bonus=10*10.0/100+10*7.5/100+(profit-20)*5.0/100;
}
else if(profit<=60)
{
bonus=10*10.0/100+10*7.5/100+20*5.0/100+(profit-40)*3.0/100;
}
else if(profit<=100)
{
bonus=10*10.0/100+10*7.5/100+20*5.0/100+20*3.0/100+(profit-60)*1.5/100;
}
else
{
bonus=10*10.0/100+10*7.5/100+20*5.0/100+20*3.0/100+40*1.5/100+(profit-100)*1.0/100;
}
printf("今年的奖金是%g\n",bonus);
}
}
还是用case吧,这么多if不好