【发布时间】: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)未使用的参数:argc2)未使用的参数:argv[]。这些可以通过int main( void )修复,我怀疑提交不能干净编译的代码是拒绝答案的好方法。 -
问题描述有这些限制:1 ≤ T ≤ 1000 1 ≤ 数量价格 ≤ 100000 所以值需要能够处理 100000*100000 即 10 000 000 000 I.E。 10 gig,但整数只能容纳 +/-2gig 所以不能使用整数进行总运算,也不能使用整数数学进行乘法运算。因此,将值读取为整数,但强制数学使用
float,然后将结果值显示为%.6f,如果您决定将每个结果保存在一个数组中,那么该数组需要包含 1000 个浮点条目。跨度>