-2

首先,我是一个早期的初学者程序员,希望得到一些帮助。

我编写了以下代码,从我测试过的代码中生成:

  • 5 个随机数:1 到 39 //num1gen 到 num5gen - eggroup A
  • 1 个介于:1 和 14 之间的随机数 //Thunderball - eggroup B

我只想按升序计算<< A组数字,我不确定添加此功能需要什么编码。

我仍然需要将生成的随机数放在下面显示的整数名称中:

IE

  • num1gen = 12
  • num2gen = 24
  • num3gen = 3
  • num4gen = 5
  • num5gen = 32
  • 雷球 = 12

错误:ISO C++ 在第 16 行禁止没有类型 [-fpremissive] 的“i”减速。

到目前为止,我在以下用户的帮助下完成了代码:

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>

using namespace std;

int main()

{

 srand(time(NULL));

std::vector<int> numA(5);
srand( time(NULL) );
for( auto i(0); i < numA.size(); ++i )    //line no 16 error
numA[i] = (rand()%49+1);

  int num1gen=(rand()%49+1);    // this is the value of ball no.1
  int num2gen=(rand()%49+1);    // this is the value of ball no.2
  int num3gen=(rand()%49+1);    // this is the value of ball no.3
  int num4gen=(rand()%49+1);    // this is the value of ball no.4
  int num5gen=(rand()%49+1);    // this is the value of ball no.5


    std::sort(numA.begin(), numA.end());

num1gen=numA[0];
num2gen=numA[1];
num3gen=numA[2];
num4gen=numA[3];
num5gen=numA[4];

 cout<<num1gen<< ", "<<num2gen<< ", "<<num3gen<< ", "<<num4gen<< ", "
 <<num5gen<< " ";"

    return 0;
  }
4

2 回答 2

2

如果您将 A 中的数字创建为向量,则会有一个带有排序的算法标题,如下所示:

#include <vector>
#include <algorithm>

int main() {
    std::vector<int> numA(5);
    srand( time(NULL) );
    for( unsigned int i(0); i < numA.size(); ++i )
        numA[i] = (rand()%49+1);
//After you create the vector and do your test that they're not equal
    std::sort(numA.begin(), numA.end());

    return 0;
}

std::sort()包含在标题#include <algorithm>中。

于 2015-02-20T19:43:37.333 回答
1
 #include <random>
 #include <vector>
 #include <algorithm>
 #include <numeric>

 int main()
{
 std::vector<unsigned> balls(39);
 std::iota(balls.begin(), balls.end(), 1);
 std::shuffle (foo.begin(), foo.end(), std::mt19937(std::random_devic{}()));
 balls.resize(5);
 std::sort(balls.begin(), balls.end());
 std::cout << "Balls: "
 std::copy(balls.begin(), balls.end(), std::ostream_iterator<unsigned>{std::cout," "})
}
于 2015-02-20T19:49:13.330 回答