20分,求助!

P1422 小玉家的电费

Hzk119 @ 2024-08-25 20:12:03

#include <iostream>
using namespace std;

int main()
{
    double a=0;
    cin>>a;
    if(a<=150)
        printf("%.1lf",a*0.4463);

    else if(a>=151 && a<=400)
        printf("%.1lf",a*0.4663);

    else 
        printf("%.1lf",a*0.5663);

    return 0;
}

by hhztl @ 2024-08-25 20:16:20

@Hzk119 分段计费不是你这样算的(

验证码qwq6祭


by LiZeChen_1 @ 2024-08-25 20:35:03

我把我以前的代码给你搞出来了,通过记录

# 输入用电总计(单位:千瓦时)
electricity_usage = int(input().strip())

# 定义电价
price_0_to_150 = 0.4463
price_151_to_400 = 0.4663
price_above_400 = 0.5663

# 初始化电费
total_fee = 0.0

# 根据用电量计算电费
if electricity_usage <= 150:
    total_fee = electricity_usage * price_0_to_150
elif electricity_usage <= 400:
    total_fee = (150 * price_0_to_150) + ((electricity_usage - 150) * price_151_to_400)
else:
    total_fee = (150 * price_0_to_150) + (250 * price_151_to_400) + ((electricity_usage - 400) * price_above_400)

# 打印结果,保留到小数点后1位
print(f"{total_fee:.1f}")

by _IOI_THANKFUL @ 2024-08-25 20:39:43

@Hzk119

参考代码

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

不是,你不知道分段计费怎么算吗?


by Hzk119 @ 2024-08-26 10:30:40

Thank you!


|