40分萌新求助!不知道哪里出错了 求大佬帮帮!

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

breadbrain @ 2023-10-30 20:40:09

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

int main()
{
    int n,k,a=0,b=0,i;
    float s=0,j=0;
    scanf("%d %d",&n,&k);
    for(i=1;i<=n;i++){
        if(i%k==0){
            s=s+i;
            a++;
        }
        else{
            j=j+i;
            b++;
        }
    }
    printf("%.1f %.1f",s/a,j/b);
    return 0;
}

by yjxlb20230 @ 2023-10-30 20:42:55

第几题?


by huanglihuan @ 2023-10-30 20:48:13

盲猜double


by cj180202 @ 2023-10-30 20:50:08

@breadbrain

float 精度太低了,很容易出现浮点误差,建议用double。(我因为这个挂过好多分


by breadbrain @ 2023-10-30 21:04:57

@cj180202 好嘞,感谢感谢!


by witherbogged @ 2023-11-01 21:02:32

建议以后存浮点数都用double,精度更高


|