c++40分求助

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

I_Wanna_AC @ 2022-09-13 19:33:03

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,k,asum=0,ai,bi,bsum=0;
double a,b;
int main(){
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            ai++;
            asum+=i;
        }
        else
        {
            bi++;
            bsum+=i;
        }
    }
    a=asum/ai;
    b=bsum/bi;
    printf("%.1lf %.1lf",a,b);
    return 0;
}

by AirQwQ @ 2022-09-13 19:38:43

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,k,asum=0,ai,bi,bsum=0;
double a,b;
int main(){
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        if(i%k==0)
        {
            ai++;
            asum+=i;
        }
        else
        {
            bi++;
            bsum+=i;
        }
    }
    a=1.0*asum/ai;//注意乘
    b=1.0*bsum/bi;
    printf("%.1lf %.1lf",a,b);
    return 0;
}

不乘1.0会自动向下取整 bgb8


by AirQwQ @ 2022-09-13 19:39:11

@I_Wanna_AC


by Ricky2008 @ 2022-09-13 20:08:31

酱紫呢?

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    int m=(1+n)*n/2,a=0,b=0;
    for(int i=k;i<=n;i+=k)a+=i,b++;
    printf("%.1lf %.1lf",a/(b*1.00),(m-a)/((n-b)*1.00));
    return 0;
}

by I_Wanna_AC @ 2022-09-14 16:02:02

过了谢谢


|