求助大佬

P1422 小玉家的电费

junbeder @ 2024-08-05 09:58:35

#include<bits/stdc++.h>
using namespace std;
int main(){
    double x;
    cin>>x;
    if(150>x)
    printf("%.1lf",x*0.4463);
    if(x>151&&x<400)
    printf("%.1lf",x*0.4663);
    if(x>401)
    printf("%.1lf",x*0.5663);
    return 0;
}```
我这代码输出的是124.5,但题目输出的是121。5,请问一下,是我错了,还是题目错了???

by Deng_jinglan @ 2024-08-05 10:02:38

#include<bits/stdc++.h>
using namespace std;
int main(){
    double a,x;
    cin>>a;
    if(a<=150){
        x=a*0.4463;
    }
    else if(a>=151&&a<=400){
        x=(a-150)*0.4663+150*0.4463;
    }
    else {
        x=(a-400)*0.5663+250*0.4663+150*0.4463;
    }
    printf("%.1f",x);
    return 0;
}

by lixuanxi @ 2024-08-05 10:08:50

@junbeder 把所有小于大于都加一个等号


by junbeder @ 2024-08-05 10:11:11

@lixuanxi 谢谢


by maomao1772 @ 2024-08-23 10:21:47

int main()
{
    double n,q=0;
    scanf("%lf",&n);
    for(int i=1;i<=n;i++)
    {
        if(i<=150)
        q+=0.4463;
    else if(i<=400&&i>150)
        q+=0.4663;
    else 
        q+=0.5663;
    }
    printf("%.1lf",q);
    return 0;
}

by 901210a @ 2024-08-29 10:41:26

求互关


|