WAWAWAWAWA

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

jzphhc @ 2024-10-29 22:54:21

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

by Fishcake_pgm @ 2024-11-15 10:36:23

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    double sum_1 = 0, sum_2 = 0;  // 用于累加 A 类和 B 类的和
    int count_1 = 0, count_2 = 0; // 统计 A 类和 B 类的数量
    double ans1, ans2;

    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        if (i % k == 0) {
            sum_1 += i;
            count_1++;
        } else {
            sum_2 += i;
            count_2++;
        }
    }

    // 用浮点数除法计算平均值
    ans1 = sum_1 / count_1;
    ans2 = sum_2 / count_2;

    // 使用 printf 输出保留一位小数
    printf("%.1lf %.1lf\n", ans1, ans2);
    return 0;
}

|