60分求助

P1422 小玉家的电费

shanyisang @ 2023-10-13 20:02:39

#include <iostream>
using namespace std;
int main(){
    int a=0;
    double b=0;
    cin>>a;
    if(a<=150){
        b=a*0.4463;
    }
    else if(150<a<=400){
        b=150*0.4463+(a-150)*0.4663;
    }
    else if(a>400){
        b=150*0.4463+250*0.4663+(a-400)*0.5663;
    }
    b=int((b*10)+0.5)/10.0;
    cout<<b<<endl;
}

求助60分


by WCX6936 @ 2023-10-13 20:11:22

b=int((b*10)+0.5)/10.0;是什么意思


by WCX6936 @ 2023-10-13 20:15:58

你没有保留小数,代码为cout<<fixed<<setprecision(n)<<b;//此题中n为1


by WCX6936 @ 2023-10-13 20:18:53

还有

else if(a>400){
        b=150*0.4463+250*0.4663+(a-400)*0.5663;
}

不需要if了,直接else


by WCX6936 @ 2023-10-13 20:19:51

@shanyisang


|