怎么爆0了!

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

44i11 @ 2022-12-09 12:32:19

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k,cnt1=0,cnt2=0;
    double sum1=0,sum2=0,ans1,ans2;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        if(i%k==0){
            sum1+=i;
            cnt1++;
        }else{
            sum2+=i;
            cnt2++;
        }
    }
    ans1=sum1/cnt1;
    ans2=sum2/cnt2;
    printf("%0.1lflf",ans1,ans2);
}

by tangrunxi @ 2022-12-09 12:34:22

@0627Xx 输出不能直接lflf,要打两次百分号。

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k,cnt1=0,cnt2=0;
    double sum1=0,sum2=0,ans1,ans2;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        if(i%k==0){
            sum1+=i;
            cnt1++;
        }else{
            sum2+=i;
            cnt2++;
        }
    }
    ans1=sum1/cnt1;
    ans2=sum2/cnt2;
    printf("%.1lf %.1lf",ans1,ans2);
}

by 44i11 @ 2022-12-09 12:35:58

@tangrunxi ,过了,谢谢!


|