首页 > 电脑

C++编程题11.用随机数产生10组36选7的号码,每组中有7个号,不能重号。排序后从小到大输出。

更新时间2018-04-29 12:40:31

参考程序

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
int m,n,t,i;
time_t ti;
srand((unsigned) time(&ti));

for(m=0; m<10; m++)
 {
  vector <int> a ;
  vector <int>::iterator it;
  n=0;
  while(n<7)
   {
    t = rand()%36+1;
    it= find(a.begin(),a.end(),t);
    if (it==a.end())  //去重复数据
     {
      a.push_back(t);
      n++;
     }
   }
  sort(a.begin(),a.end()); //排序
  for(i=0; i<7; i++)
   {
    cout << a[i] << " ";
   }
  cout << endl;
 }
return 0;
}

测试结果

3 5 7 11 15 22 23
1 17 20 22 26 31 35
2 12 15 17 21 34 35
5 12 15 18 24 26 30
2 9 11 15 22 24 28
1 4 8 12 15 30 34
2 4 6 9 18 20 26
3 6 20 23 24 31 33
5 6 9 14 27 33 35
3 13 21 23 26 27 32

相关标签:号码

上一篇:西门子编程中转至在线后怎样让一段程序运行?

下一篇:打字的程序