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

· · 题解

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

Update 2025.09.20:原程序中变量 x 没有使用,已删除。

1. 题目大意

2. 思路

3. 代码

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k,s1=0,s2=0;
    double c1=0,c2=0;
    cin>>n>>k;
    for (int i=1;i<=n;i++)
    {
        if (i%k==0) //是否为 k 的倍数
        {
            s1++;
            c1+=i;
        }
        else
        {
            s2++;
            c2+=i;
        }
    }
    printf("%.1lf %.1lf",c1/s1,c2/s2); //或 cout<<fixed<< setprecision(1)<<c1/s1<<" "<<c2/s2;
    return 0;
}

4. 总结

谢谢观赏!
若有不完善的地方还请各位大佬指出!