码迷,mamicode.com
首页 > 编程语言 > 详细

随机排列数组-algorithms_3th

时间:2015-02-28 18:22:23      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <iostream>
 2 #include <random>
 3 #include<iterator>
 4 using namespace std;
 5 
 6 int rand_int(const int &up, const int &lo){
 7     uniform_int_distribution<> ran(up, lo);
 8     random_device rd;
 9     mt19937 gen(rd());
10     return ran(gen);
11 }
12 
13 void swap(int &a, int &b){
14     int t = a;
15     a = b;
16     b = t;
17 }
18 
19 int main(void)
20 {
21     int a[] = { 1,2,3,4,5,6,7,8,9 };
22     for (auto i = 0; i < end(a) - begin(a); ++i){
23         cout << a[i] << " ";
24     }
25     cout << endl;
26     for (auto i = 0; i < end(a) - begin(a); ++i){
27         swap(a[i], a[rand_int(i, end(a) - begin(a)-1)]);
28     }
29     for (auto i = 0; i < end(a) - begin(a); ++i){
30         cout << a[i] << " ";
31     }
32     cout << endl;
33     system("pause");
34     return 0;
35 }

 

随机排列数组-algorithms_3th

标签:

原文地址:http://www.cnblogs.com/lhyz/p/4305757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!