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 }
时间: 2024-10-20 03:06:33