Advertisement
Guest User

2

a guest
Mar 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <time.h>
  2. #include <iostream>
  3. #include <functional>
  4. #include <random>
  5. #include <ctime>
  6. #include <map>
  7. #include <ratio>
  8. #include <chrono>
  9. using namespace std;
  10. using namespace std::chrono;
  11. int main()
  12. {
  13.     map<long long int, int> map0;   //儲存各種排列出現的次數
  14.     random_device rd;       //設定PRNG
  15.     auto tt = chrono::high_resolution_clock::now();     //亂數種子為當下時間
  16.     std::mt19937_64 gen = std::mt19937_64(tt.time_since_epoch().count());       //以梅森旋轉作為PRNG
  17.     std::uniform_int_distribution<> dis(0, 1);  //數值範圍限定為0或1
  18.     auto Rand = bind(dis, gen);
  19.     int N = 1;
  20.     while (N < 50000) {     //創造一個夠大但隨機的N,為了程式碼乾淨使用不再多寫一個PRNG
  21.         if (Rand())
  22.             N = N * 2 + 1;
  23.         else
  24.             N *= 2;
  25.     }
  26.     int T = N, n, location[1000];
  27.     cout << "將會執行" << T << "次" << endl;
  28.     cin >> n;       //輸入人數
  29.    
  30.     while (T--)
  31.     {
  32.         for (int i = 0; i < n; i++)
  33.             location[i] = i + 1;
  34.         for (int i = 0; i < n; i++)
  35.             for (int j = i+1; j < n; j++)
  36.                 if (Rand())
  37.                     swap(location[i], location[j]);
  38.  
  39.         long long int number = 0;       //把排列轉換成數字方便儲存
  40.         for (int i = 0; i < n; i++)
  41.             number = number * n + location[i];
  42.         map0[number]++;
  43.     }
  44.  
  45.     long long int max0 = 0, min0 = N, size0 = 1;    //極大極小
  46.     long double S = 0;  //標準差
  47.     for (int i = 1; i <= n; i++)
  48.         size0 *= n;
  49.     for (auto iter = map0.begin(); iter != map0.end(); iter++) {
  50.         max0 = max(max0, (long long int)iter->second);
  51.         min0 = min(min0, (long long int)iter->second);
  52.         S += pow((iter->second - (double)N / size0), 2);
  53.     }
  54.     S /= size0;
  55.     S = sqrt(S);
  56.     cout << max0 << " " << min0 << endl;
  57.     cout << S << endl;
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement