請仔細閱讀任務第一:http://codeabbey.com/index/task_view/neumanns-random-generator諾伊曼的隨機數發生器
我必須跟蹤迭代的次數,但我得到非常奇怪的結果。在任務之後的例子中,我們有數字0001和4100,並且它們應該在2次和4次迭代之後循環。但我的結果是1,4或者如果我改變計數器的地方2或5,但從來沒有2和4。這裏是我的代碼:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
int value;
int counter;
int result;
int setvalue = 1; // use to exit the loop if setvalue == 0;
cin >> n;
vector<int> new_results(0); // use to store all the results from iterations
vector<int> results_vec(0); // use to store the number of iterations for each number
for (int i = 0; i < n ; i++)
{
cin >> value;
while(setvalue == 1)
{
value = value*value;
value = (value % 1000000)/100;
if(find(results_vec.begin(), results_vec.end(), value) == results_vec.end())
{
results_vec.push_back(value);
}
else
{
counter = results_vec.size();
new_results.push_back(counter);
setvalue = 0;
}
}
results_vec.clear();
}
for (int i = 0; i < new_results.size() ; i++)
{
cout << new_results[i] << " ";
}
}
如果您以0001和4100開頭,會生成哪些中間數字? – tgmath
對於0001我得到(0,0) 對於4100我得到(8100,6100,2100,4100) – Vallerious
在開始while循環之前你不應該將初始種子值放入'results_vec'嗎? – pjs