请问各位大佬们,为什么就四十分啊

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

zlh202483023 @ 2024-11-21 16:13:29

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
    int n, k, sum, num, count1, count2;
    cin >> n;
    cin >> k;
    sum = 0;
    num = 0;
    count1 = 0;
    count2 = 0;
    for (int i = 1; i <= n; i++) {
        if (i % k == 0) {
            sum = sum + i;
            count1 ++;
        }
        else if (i % k != 0) {
            num = num + i;
            count2 ++;
        }
    }
    cout << fixed << setprecision(1) << double(sum / count1) << " " << fixed << setprecision(1) << double( num / count2 );
    return 0;
}

by Fcersoka @ 2024-11-21 16:30:12

@zlh202483023你应该把 sum 和 num 都转换为 double 类型再进行计算。


by zlh202483023 @ 2024-11-21 16:56:37

@Fcersoka改了还是四十分苦笑


by Fcersoka @ 2024-11-21 17:56:52

@zlh202483023改成这样

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
    int n, k, count1, count2;
    double sum,num;
    cin >> n;
    cin >> k;
    sum = 0;
    num = 0;
    count1 = 0;
    count2 = 0;
    for (int i = 1; i <= n; i++) {
        if (i % k == 0) {
            sum = sum + i;
            count1 ++;
        }
        else if (i % k != 0) {
            num = num + i;
            count2 ++;
        }
    }
    cout << fixed << setprecision(1) << double(sum / count1) << " " << fixed << setprecision(1) << double( num / count2 );
    return 0;
}

by zlh202483023 @ 2024-11-24 09:49:06

@Fcersoka谢谢大佬!!!


by luogu_00 @ 2024-11-25 21:22:21

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
    int n, k, count1 = 0, count2 = 0;
    double sum = 0, num = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        if (i % k == 0) {
            sum = sum + i;
            count1 ++;
        }
        else {
            num = num + i;
            count2 ++;
        }
    }
    printf("%.1lf %.1lf", sum / count1, num / count2);
    return 0;
}

|