题目中的:
货物重量≤5公斤,快递费收3元
应该为
货物重量≤5公斤,快递费收3元/公斤
#include
int a[17]={-1,0,3,5,6,10,12,18,20,25,30,35,40,50,55,57,60};// 测试数据
int main()
{
int i=17;
int zhongliang;
float flg;
while(i)
{
i--;
//scanf("%d",&zhongliang);
zhongliang=a[i];
flg=0;
switch(zhongliang/5)
{
case 6:case 7:case 8:case 9: case 10:
flg += (float)(5*(zhongliang-30)); zhongliang=30;
case 4:
case 5: flg += (float)(4.5*(zhongliang-20));zhongliang=20;
case 2:
case 3: flg += (float)(4*(zhongliang-10)); zhongliang=10;
case 1: flg += (float)(3.5*(zhongliang-5)); zhongliang=5;
case 0: flg += (float)(3*zhongliang); break;
default: flg = -1;
}
if(flg>=0)
printf("%2d -- %0.2f \n",a[i],flg);
else
printf("%2d -- No! \n",a[i]);
}
//getch();
}
呵呵 楼上写的不错。
不过从实际角度来说,这个题目用循环写最简单。
#include
int main()
{
int weight;
float price;
while(scanf("%d",&weight)!=EOF)
{
switch(weight/5)
{
case 0: price = 3*weight ;break;
case 1: price = 3*5+3.5*(weight-5);break;
case 2:
case 3:
price = 6.5*5+4*(weight-10);break;
case 4:
case 5:
price = 6.5*5+40+4.5*(weight-20);break;
case 6:case 7:case 8:case 9:
price =6.5*5+40+45+5*(weight-30);break;
default:
price = -1;
}
if(price>0) printf("price is $f \n",price);
else print("refused!\n");
}
return 0;
}