80分求助,我写的为啥不对鸭.

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

Si_Ji_Lao @ 2023-01-20 13:51:58

#include <iostream>
#include <iomanip>
using namespace std;
int n1;
int n2;
int num;
int k;
int n11, n22;
int main() {
    cin >> num;
    cin >> k;
    for (int i = 2; i < num; i++)
    {
        if (i%k==0)
        {
            n1 += i;
            n11 += 1;
        }
        else
        {
            n2 += i;
            n22 += 1;
        }

    }
    cout << fixed << setprecision(1) << double(n1) / double(n11) << " " << double(n2) / double(n22);
    return 0;
}

by 邦邦家的雷达站 @ 2023-01-20 14:11:38

for循环中的i要从1开始定义


by 邦邦家的雷达站 @ 2023-01-20 14:12:51

即for (int i = 1; i < =num; i++)


by 邦邦家的雷达站 @ 2023-01-20 14:15:55

注意看题目中“将从 1 到 n 之间的数”,因此不仅i要定义为1,还要<=num


|