欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C++生成不重復(fù)的隨機整數(shù)

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

C++生成不重復(fù)的隨機數(shù),供大家參考,具體內(nèi)容如下

給定正整數(shù)的范圍[n,m],生成k個不重復(fù)的隨機數(shù)字。

IDE是vs013。

#include "stdafx.h"
#include <iostream> 
#include <vector>
#include <stdlib.h> 
#include <time.h>
#include<list>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
 srand((unsigned)time(NULL));
 list<int>::iterator it;//迭代器
 list<int> l;//定義鏈表,保存生成的隨機數(shù)
 int begin, end;//數(shù)字范圍
 int sum;//隨機數(shù)個數(shù)
 cout << "輸入數(shù)字范圍([n,m]):";
 cin >>begin>>end;
 cout << "輸入隨機數(shù)個數(shù):";
 cin >> sum;
 if ( (end<0)||(begin<0)||(begin >end)|| (sum>end))//起始范圍必須大于0,且隨機數(shù)個數(shù)小于等于最大數(shù)字范圍
 {
 cout << "范圍錯誤";
 cout << endl;
 system("pause");
 return 0;
 }
 else
 {
 while (l.size() < sum)
 {
 l.push_back(rand() % (end - begin + 1) + begin);
 l.sort();//排序
 l.unique();//去除相鄰的重復(fù)隨機數(shù)中的第一個
 }
 cout << "結(jié)果:";
 }
 for (it = l.begin(); it != l.end(); it++)
 {
 cout << *it << ' ';
 }
 
 cout << endl;
 system("pause");
 return 0;
}

運行結(jié)果:

這個程序可以用于班級內(nèi)部按照學(xué)號進行隨機抽簽。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C++實現(xiàn)大數(shù)相乘的算法

欄    目:C語言

下一篇:利用C++ R3層斷鏈實現(xiàn)模塊隱藏功能

本文標題:C++生成不重復(fù)的隨機整數(shù)

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/189.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有