0分,求助!!!

P1422 小玉家的电费

small_Dongpo @ 2023-02-11 14:18:37

我的代码:

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int n;
    double s = 0;
    cin >> n;
    if (n <= 150)
    {
        s += n * 0.4463;
    }
    else if (n >= 151 && n <= 400)
    {
        s += 150 * 0.4463;
        s += (n - 150) * 0.4663;
    }
    else
    {
        s += 150 * 0.4463;
        s += (400 - 150)  * 0.4663;
        s += (n - 400) * 0.5663;
    }
    printf("%2.lf", s);
    return 0;
}

by anata @ 2023-02-11 14:24:47

@Howson_20120405

输出格式不对,应该是"%.1lf"


by __QHY__ @ 2023-02-11 14:25:32

这里:


 printf("%2.lf", s);

by __QHY__ @ 2023-02-11 14:26:15

应该为 printf(".1.lf", s);


by __QHY__ @ 2023-02-11 14:26:46

改了就能AC了


by __QHY__ @ 2023-02-11 14:28:15


#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int n;
    double s = 0;
    cin >> n;
    if (n <= 150)
    {
        s += n * 0.4463;
    }
    else if (n >= 151 && n <= 400)
    {
        s += 150 * 0.4463;
        s += (n - 150) * 0.4663;
    }
    else
    {
        s += 150 * 0.4463;
        s += (400 - 150)  * 0.4663;
        s += (n - 400) * 0.5663;
    }
    printf("%.1lf", s);
    return 0;
}

by __QHY__ @ 2023-02-11 14:28:46

不信你试试


by __Tonycyt__ @ 2023-02-11 15:00:48

@Howson_20120405
printf("%.1lf",s);题目有说


by __Tonycyt__ @ 2023-02-11 15:01:50

输出一个数,保留到小数点后 1 位(单位以元计,保留到小数点后 1 位)。


by small_Dongpo @ 2023-02-14 17:42:28

谢谢


by Exile_Code @ 2023-05-24 19:21:35

@q1haoyu %.1llf和%.1lf不是一样的吗?? 我也犯了这个问题,这是为什么呢


|