help!

P1422 小玉家的电费

Sx0528 @ 2024-09-08 20:49:21

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    double sum=0;
    cin>>n;
    if(n<=150) sum+=n*0.4463;
    else if(n<=400) sum+=150*0.4463+(n-150)*0.4663;
    else sum+=150*0.4463+400*4663+(n-400)*0.5663;
    cout<<fixed<<setprecision(1)<<sum<<endl;
    return 0;
}

by zhanghm @ 2024-09-08 20:56:10

else sum+=150*0.4463+400*4663+(n-400)*0.5663;改成else sum+=150*0.4463+400*0.4663+(n-400)*0.5663;


by GYZZZZZZ @ 2024-09-08 20:57:02

else那行改成else cout<<fixed<<setprecision(1)<<150 0.4463 + 250 0.4663 + (n - 400) * 0.5663;再试试?


by yyycj @ 2024-09-08 20:58:45

最后 n > 400 的情况 sum+=150*0.4463+400*4663+(n-400)*0.5663 中的 400*4663 不对,151 \sim 400 相当于只有 250 千瓦时,并且应该乘 0.4663。应改为 sum+=150*0.4463+250*0.4663+(n-400)*0.5663


by wuyixuan0801 @ 2024-09-10 21:27:19

代码:electricity = int(input()) cost = 0 if electricity <= 150: cost = electricity 0.4463 elif 151 <= electricity <= 400: cost = 150 0.4463 + (electricity - 150) 0.4663 else: cost = 150 0.4463 + 250 0.4663 + (electricity - 400) 0.5663 print(f'{cost:.1f}')


by wuyixuan0801 @ 2024-09-10 21:27:59

Python 语言


by wuyixuan0801 @ 2024-09-10 21:28:28

%100过


|