这道题该怎么思考呢?请大家帮我一下。可以吗?
这道题该怎么思考呢?请大家帮我一下。可以吗?
float DisCount( float price)
{
if(price <= 0)
return 0;
if(price > 1000)
price = price * 0.9;
else if( price > 500)
price = price * 0.95;
else if( price > 100)
price = price * 0.98;
return price;
}
[此贴子已经被作者于2007-6-3 19:25:18编辑过]
抄袭楼上的
#include<stdio.h>
main()
{
float price;
printf("Please input a number");
scanf("%f",&price);
{
if(price <= 0)
return 0;
if(price > 1000)
price = price * 0.9;
else if( price > 500)
price = price * 0.95;
else if( price > 100)
price = price * 0.98;
else if( price > 0)
price = price;
printf("%.2f",price);
}
getch();
}
思考方法:
请用户输入一个金额;
if(金额小于0)
提示输入错误;(当然这里也可以不给予理会``想要怎么样```就看你自己了)
else if (金额>0且<=100)
输出原金额;
else if (金额>100且<=500)
输出金额的%98;
else if (金额>500且<1000)
输出金额的%95;
else id (金额>=1000)
输出金额的%90;
当然``if else 可以用 switch()语句``换掉
用swicth语句可以十分简单解决
#include<stdio.h>
main()
{ int n;float s;
printf("please input a number:"); /*输入一个大于0的正整数*/
scanf("%d",&n);
swicth(n/100)
{ case 0:printf("%d",n);break;
case 1:
case 2:
case 3:
case 4:
case 5:printf("s=%5f",s=0.98*n);break;
case 6:
case 7:
case 8:
case 9:
case 10: printf("s=%5f",s=0.95*n);break;
default:printf("s=%5f",s=0.9*n);break;
}
}