【问题标题】:Codechef Total ExpensesCodechef 总费用
【发布时间】:2015-12-05 17:09:09
【问题描述】:

我正在使用下面的代码来解决https://www.codechef.com/problems/FLOW009的问题

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int T;
    float quantity, price, tex;
    scanf("%d", &T);

    while(T--)
    {
        scanf("%f %f", &quantity, &price);

        tex = quantity * price;
        if (quantity > 1000)
            tex = tex - (tex * 0.1);

        printf("%.6f\n", tex);
    }

    return 0;
}

我不知道为什么这总是给我错误的答案。

我尝试更改数据类型。

int main(int argc, char const *argv[])
{
    int T, quantity, price;
    float tex;
    scanf("%d", &T);

    while(T--)
    {
        scanf("%d %d", &quantity, &price);

        tex = quantity * price;
        if (quantity > 1000)
            tex -= (tex * 0.1);

        printf("%.6f\n", tex);
    }

    return 0;
}

但这也给出了错误的答案。

【问题讨论】:

  • 你得到了什么“错误”的答案?
  • 当我使用 Sample Input 对其进行测试时,它给出了预期的结果,但在线法官显示“错误答案”。
  • 顺便说一句:关于这个表达式:(tex * 0.1); 0.1 是一个双重文字。你实际上想要一个浮点文字,即0.1f
  • 发布的代码无法干净地编译。总是在启用所有警告的情况下编译。 (对于 gcc,至少使用:-Wall -Wextra -pedantic)然后编译器会告诉你 1)未使用的参数:argc 2)未使用的参数:argv[]。这些可以通过int main( void ) 修复,我怀疑提交不能干净编译的代码是拒绝答案的好方法。
  • 问题描述有这些限制:1 ≤ T ≤ 1000 1 ≤ 数量价格 ≤ 100000 所以值需要能够处理 100000*100000 即 10 000 000 000 I.E。 10 gig,但整数只能容纳 +/-2gig 所以不能使用整数进行总运算,也不能使用整数数学进行乘法运算。因此,将值读取为整数,但强制数学使用float,然后将结果值显示为%.6f,如果您决定将每个结果保存在一个数组中,那么该数组需要包含 1000 个浮点条目。跨度>

标签: c algorithm


【解决方案1】:

在问题中指出,包括数量和价格在内的所有输入都是整数。您已将它们声明为浮点数,这可以更改您对数量大于 1000 的输入的一些答案,如下所示:

1
1001 8
7207.200195 (your ans, correct = 7207.200000)

由于浮点值。修改后的代码如下所示

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int test, qty, price;
    double total;

    scanf("%d", &test);

    while(test--)
    {
        scanf("%d %d", &qty, &price);

        total = qty * price;

        if (qty > 1000)
            total -= ((total * 10.0)/100.0);

        printf("%.6lf\n", total);
    }

    return 0;
}

【讨论】:

  • 我试过了。将数量和价格变量的数据类型更改为浮点类型不起作用。
  • 将它们更改为整数并将 tex 保留为浮点数
  • 是的,我将 tex 保留为浮点数
  • 您可以在我给出的示例输入上运行这两个代码,它将显示不同的输出。
  • @Weather Vane 问题中的代码和我给出的答案都是一样的,没有逻辑上的区别。
【解决方案2】:

您不必在每个输入集之后都提供输出。这就是您的答案被拒绝的原因。将输出存储在数组中并在最后打印。

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int T, quantity, price, i;
    double *tex = NULL;
    scanf("%d", &T);

    tex = (double *)malloc(sizeof(double) * T);

    for(i=0; i<T; i++)
    {                                 
        scanf("%d %d", &quantity, &price);

        tex[i] = (float)(quantity * price);
        if (quantity > 1000)        
            tex[i] -= (tex[i] * 0.1);

    }                     

    for(i=0; i<T; i++)
        printf("%.6lf\n", tex[i]);

    return 0;
}        

【讨论】:

  • 更改为 double 不会解决问题,因为 1) 所有输入都是整数 2) doublefloat 有相同类型的 exact number representation 问题
  • 在 C 中调用malloc(), 1) 时,返回值为void*,因此可以分配给任何其他指针。转换返回值会产生不必要的问题。 2) 始终检查 (!=NULL) 返回值以确保操作成功。即使在最好的环境中,也可能发生内存分配错误。
  • @user3629249 1.) 如果 malloc 没有被调用,那么我们必须修复分配给 tex 的内存大小,这看起来像是内存浪费。 2.) 由于代码要提交给codechef,所以我没有考虑内存分配失败的情况。
  • 我从未说过“不要使用malloc()”。我确实说过要执行错误检查。我确实说过不要从malloc() 转换返回值
【解决方案3】:

iomanip 头文件中有一个名为 setprecision() 的函数,它允许您设置要获得结果的小数位数。打印最后一位需要您编写这样的语句 -

cout<<fixed<<setprecision(6)<<total<<endl;

【讨论】:

  • 对不起,我一开始没有注意到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2021-02-10
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
相关资源
最近更新 更多