#include <iostream> #include <map> #include <vector> #include <iomanip> using namespace std; int main(){ int x; map<int,bool> m; cin >> x; while( (x=((x*x)/10)%1000) && !m[x]){ m[x]=true; } cout << m.size()+1; system("pause"); } /* 123 => 14 172 => 17
輸入: 3 位數利用平方取中法產生亂數規則: 取萬位數到十位數輸出: 產生到第幾個數的時候開始出現重複?範例輸入:123輸出: 14計算過程
| 1 | 512 |
| 2 | 214 |
| 3 | 579 |
| 4 | 524 |
| 5 | 457 |
| 6 | 884 |
| 7 | 145 |
| 8 | 102 |
| 9 | 40 |
| 10 | 160 |
| 11 | 560 |
| 12 | 360 |
| 13 | 960 |
| 14 | 160 |
| 15 | 560 |
| 16 | 360 |
| 17 | 960 |
| 18 | 160 |
| 19 | 560 |
沒有留言:
張貼留言