#include<stdio.h>
int main()
{
int x,y,z,sum,b,n;
double price,first,second,three;
scanf("%d %d",&x,&y);
b=x;n=y;
sum=x+y;
if(sum<=2760){
price=0.538*y;
printf("%.2f",price);
}
else if(2760<sum&&sum<=4800){
if(x<=2760)
{
z=2760-x;
b=y-z;
first=0.538*z;
second=0.588*b;
price=first+second;
printf("%.2f",price);
}
else{
price=y*0.588;
printf("%.2f",price);
}
}
else if(sum>4800)
{
if(x<=2760){
b=2760-x;
first=0.538*b;
second=0.588*2040;
z=y-4800;
three=z*0.838;
price=first+second+three;
printf("%.2f",price);
}
else if(x>2760&&x<=4800){
b=4800-x;
first=0.588*b;
z=y-b;
second=0.838*z;
price=first+second;
printf("%.2f",price);
}
else if(x>4800)
{
first=y*0.838;
printf("%.2f",first);
}
}
}
你的逻辑写得太复杂了,应该从最高档往下算比较简洁些
#include <stdio.h>
double calculate_fee(double x) {
double bands[] = {2760.0, 4800.0};
double rates[] = {0.538, 0.588, 0.838};
double sum = 0.0;
if (x > bands[1]) {
sum += (x - bands[1]) * rates[2];
x = bands[1];
}
if (x > bands[0]) {
sum += (x - bands[0]) * rates[1];
x = bands[0];
}
sum += x * rates[0];
return sum;
}
int main() {
double x, y;
scanf("%lf%lf", &x, &y);
printf("%.2lf", calculate_fee(x + y) - calculate_fee(x));
return 0;
}