为什么样例没过?

P1422 小玉家的电费

Handsome_Brother_Boy @ 2024-06-15 09:12:14

#include<bits/stdc++.h>
using namespace std;
long long n,a[10001],maxn;
double s;
int main(){
    cin>>n;
    if(n<=150)
    {
        s+=n*0.4463;
    }
    else
    {
        s+=150*0.4463;
        if(n<=400)
        {
            s+=(n-150)*0.4663;
        }
        else
        {
            s+=(n-400)*0.5663;
        }
    }
    printf("%0.1d",s);
    return 0;
}

by liuli688 @ 2024-06-15 09:23:07

@Handsome_Brother_Boy 如果用电大于 400千瓦时,那么应该分 3 段收费:0 \sim 150150 \sim 400,大于 400

我的 std:

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

by Handsome_Brother_Boy @ 2024-06-15 09:26:38

@liuli688 唉,我的if写的太乱了


by YU_Accepted @ 2024-06-15 09:38:39

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

|