二,三,四点错了,求大佬解答一下

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

116147zqq @ 2022-11-04 21:57:40

#include <stdio.h>

int main()
{
    int n,k,i,a,b,A,B;
    float aver1,aver2;
    scanf("%d%d",&n,&k);
    i=1;
    a=0;
    b=0;
    A=0,B=0;
    while(i<=n)
    {

        if(i%k==0)
        {
           a=a+i;
           A++;
        }
        else
        {
            b=b+i;
            B++;
        }
        i++;
    }

    aver1=a/A;
    aver2=b/B;
    printf("%.1f %.1f",aver1,aver2);

    return 0;
}

by hy233 @ 2022-11-04 22:07:37

c++在int/int时默认使用整除(即忽略余数),可以强制类型转换或在前面*1.0


by hy233 @ 2022-11-04 22:07:59

@116147zqq


by 116147zqq @ 2022-11-04 22:16:12

@hy233 感谢大佬


by qq1715505656 @ 2022-11-06 11:48:32

@116147zqq 怎么解决的老铁?


by 116147zqq @ 2022-11-06 21:54:40

@qq1715505656 在a/A和b/B前面乘上1.0就行


|