2013年11月18日 星期一

平方取中法=>到第幾個數開始出現重複?

#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
計算過程
1512
2214
3579
4524
5457
6884
7145
8102
940
10160
11560
12360
13960
14160
15560
16360
17960
18160
19560
*/

沒有留言:

張貼留言