求大佬指点错误!!!数据不对 感觉逻辑没问题 2.3.4点错误

P5719 【深基4.例3】分类平均

Gain @ 2021-12-16 09:36:42

#include<stdio.h>
int main()
{
    int n,k,number1=0,number2=0,res1=0,res2=0;
    double Res1,Res2;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            res1=res1+i;
            number1++;
        }
        else
        {
            res2=res2+i;
            number2++;
        }
    }
    Res1=res1/number1;
    Res2=res2/number2;
    printf("%.1lf %.1lf",Res1,Res2);
    return 0;
}

by 编码落寞 @ 2021-12-16 10:21:15

@ohhhhhh11 Res1=res1/number1;

这里是两个整数相除,结果会直接进位。

转成double后,就是后面填个0


by Gain @ 2021-12-16 21:07:40

@编码落寞 好的大佬AC了


|