我是否正确使用srand功能?

时间:2018-06-14 03:22:12

标签: c++

当我运行该程序时,它可以运行,但是一旦我针对测试用例提交了代码,它们都会失败。

继承我所拥有的。

#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;

int classA[10];
int classB[10];

int main()
{
    int seedValue;
    cin >> seedValue;
    srand(seedValue);

    int i;
    double sumA = 0;
    double sumB = 0;
    double avgA;
    double avgB;

    for (i = 0; i <= 10; ++i)
    {
        classA[i] = rand() % 101;
        classB[i] = rand() % 101;
    }

    for (i = 0; i <= 10; ++i)
    {
        sumA = sumA + classA[i];
        sumB = sumB + classB[i];
    }
    avgA = sumA / 10;
    avgB = sumB / 10;

    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << "Class A average is: " << avgA << endl << "Class B average is: " << avgB << endl;

    if (avgA < avgB)
    {
        cout << "Class B wins!" << endl;
    }
    else
    {
        cout << "Class A wins!"<< endl;
    }

    system("Pause");
    return 0;
}

输入1我的程序返回

Class A average is: 46.50
Class B average is: 47.00
Class B wins!
sh: 1: Pause: not found

然而,它应该看起来像这样......

Class A average is: 44.00
Class B average is: 41.20
Class A wins!

我觉得生成相同的数字会导致输出不同。任何帮助都会很棒,谢谢。

0 个答案:

没有答案