我的暴零,哪位大佬看看

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

tyx20101117 @ 2022-12-04 12:52:14

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k,i,as,bs,at,bt;
    double a,b;
    cin>>n>>k;
    for (i=1;i<=n;i++){
        if (i%k==0){
            at++;
            as+=i;
        }
        else{
            bt++;
            bs+=i;
        }
    }
    a=as*1.0/at;
    b=bs*1.0/bt;
    printf("%.1lf %.1lf",a,b);
} 

by Glooda @ 2022-12-04 13:02:36

as,bs,at,bt转成double试试?


by Glooda @ 2022-12-04 13:05:35

把上述四个变量改成double数据类型,亲测已AC


by Glooda @ 2022-12-04 13:06:49

改后代码


using namespace std;
int main(){
    int n,k,i;
    double a,b,as,bs,at,bt;
    cin>>n>>k;
    for (i=1;i<=n;i++){
        if (i%k==0){
            at++;
            as+=i;
        }
        else{
            bt++;
            bs+=i;
        }
    }
    a=as*1.0/at;
    b=bs*1.0/bt;
    printf("%.1lf %.1lf",a,b);
} 

by Enmin @ 2023-01-17 15:03:18

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n,k,a=0,b=0,sum=0,ans=0;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) {
        if(i%k==0)
            a++;    
        if(i%k!=0)
            b++;
        if(i%k==0)
            sum=sum+i;
        if(i%k!=0) 
            ans=ans+i;
    }
    printf("%.1lf",sum/a);
    cout <<" ";
    printf("%.1lf\n",ans/b);     
    return 0;
}

by Enmin @ 2023-01-17 15:04:38

我的暴零,哪位大佬看看


|