BenChaut @ 2022-10-12 21:46:07
in;10000 19;out:5006.5,5000.2而我out:5006.5,5000.0
#include <stdio.h>
int main()
{
int n,k, a = 0, b = 0; float sum1 = 0; float sum2 = 0;
scanf("%d %d", &n,&k);
for(int i = 1; i <= n; i++)
{
if (i %k== 0)
{
sum1 += i;
a++;
}
else
{
sum2 += i;
b++;
}
}
printf("%.1f %.1f", sum1 / a, sum2 / b);
return 0;
}
by Apress @ 2022-10-12 21:51:38
<<你为什么不用double>>
by Apress @ 2022-10-12 21:52:05
@BenChaut
by RP_INT_MAX @ 2022-10-12 21:52:07
@BenChaut
#include <stdio.h>
int main()
{
int n,k, a = 0, b = 0; double sum1 = 0, sum2 = 0;
scanf("%d %d", &n,&k);
for(int i = 1; i <= n; i++)
{
if (i %k== 0)
{
sum1 += i;
a++;
}
else
{
sum2 += i;
b++;
}
}
printf("%.1lf %.1lf", sum1 / a, sum2 / b);
return 0;
}```
by RP_INT_MAX @ 2022-10-12 21:52:37
float 会爆精,用 double.
by BenChaut @ 2022-10-12 23:48:03
@RP_INT_MAX 为什么呢,float不是可以达到小数点后6位了吗,题目不是要求精确到小数点后一位就好吗,刚入门不是很理解呢,大佬可以详细说一下吗
by BenChaut @ 2022-10-12 23:50:41
@Apress 新手不懂为什么要用double,float不是可以到小数点后6位不是可以满足题目一位小数的精度了吗,大佬可不可以详细说说呢
by BBY123 @ 2022-10-25 20:30:45
@BenChaut 题目说精确到一位小数,但没有说算出来的精确值是一位小数,可能有几十位小数,所以要用double。我建议你一直用double,否则爆精了都不知道错出在哪里(除题目特别要求外)