gpt4 book ai didi

c++ - 诺依曼的随机生成器

转载 作者:行者123 更新时间:2023-11-28 02:55:16 25 4
gpt4 key购买 nike

请先阅读任务: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] << " ";
}

}

最佳答案

以您现有的方式进入和退出字符串确实非常丑陋,而且计算成本极其高昂。

使用

(值 % 1000000)/100;

取而代之的是提取中间四位数字。这通过 (1) 取模删除前两位数字然后 (2) 通过整数除法删除最后两位数字来实现。

因为它简单得多,我怀疑它也能修复您的错误。

关于c++ - 诺依曼的随机生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22146596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com