题主可以参考下面这种写法:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int weight,cost=8;
char quick;
cin>>weight>>quick;
if(weight>1000)
{
weight -= 1000;
cost += weight/500*4;
if(weight%500!=0)
{
cost += 4;
}
}
if(quick == 'y')
{
cost += 5;
}
cout<<cost<<endl;
return 0;
}
运行结果如图:
解释:
①名词解释:weight:总重量;cost:总费用;quick:是否加急
②代码思路解释:当重量大于1000克时,我们会拿总重量-1000代表超出的重量,根据超出的重量进行判断(weight是否能被500整除),当超出的重量刚好是500的倍数的时候weight/500*4。如果最后超出重量不能被500整除那么cost直接加4,计算完重量邮资后,看是否需要加急,如果需要加急,cost加5,如果不需要加急,cost则不需要加5。
以上仅供参考,如有任何疑问,可以评论回复,看到即回。
希望对题主有所帮助!可以的话,点个采纳!
修改如下,供参考。代码里 m==89 或 m==78 ,这两个ASCII值是大写的 ’Y‘ 和 ’N‘ ,所以输入时:1200 Y 。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
char m;
cin >> n >> m;
if (n <= 1000) {
if (m == 89)cout << 13 << endl;
else if (m == 78)cout << 8 << endl;
}
if (n > 1000) {
if (m == 89)cout << ceil((n - 1000) * 1.0 / 500) * 4 + 13 << endl;
else if (m == 78)cout << ceil((n - 1000) * 1.0 / 500) * 4 + 8 << endl;
}
return 0;
}