作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请先阅读任务: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/
我正在尝试为我的应用执行“部署:冷”。 git repo 是本地的我的部署服务器(即我只有一台服务器用于所有内容,而且我不要在 github 上托管我的代码)。 这是成绩单(出于隐私考虑,将我的应用名
我是一名优秀的程序员,十分优秀!