谁能帮我看看哪里错了,大佬们

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

guang_zi_guei_ji @ 2024-04-27 19:53:36

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k;
    float a=0,b=0,N,a1=0,b1=0;
    scanf("%d%d",&n,&k);
    N=n;
    for(int i=1;i<=n;i++){
        if(i%k==0){
            a++;
            a1+=i;
        }
        else{
            b++;
            b1+=i;
        }
    }
    printf("%.1f %.1f",a1/a,b1/b);
    return 0;
}

by guang_zi_guei_ji @ 2024-04-27 19:56:27

后面三个都wa了


by szlh_XJS @ 2024-04-27 20:00:25

@guang_zi_guei_ji 试试把所有的变量都改成float


by guang_zi_guei_ji @ 2024-04-27 20:04:00

@szlh_XJS 不行啊,要取余


by guer_loser_lcz @ 2024-04-27 20:19:40

@guang_zi_guei_ji 改成double?


by philip2011 @ 2024-04-27 20:22:26

#include<iostream>
using namespace std; 
int main()
{
    int n,k,a=0,b=0;
    double suma=0,sumb=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        if(i%k==0){
            a++;
            suma+=i;
        }
        else if(i%k!=0){
            b++;
            sumb+=i;
        }
    }
    printf("%.1lf %.1lf",suma/a,sumb/b);
    return 0;
}

by guang_zi_guei_ji @ 2024-04-27 20:32:50

@lczcy1 谢谢指导


by llhhss @ 2024-09-05 21:20:30

@szlh_XJS float算整形变量,整形变量不包括取余


|