这个C++代码有什么问题?3n+1 在编程挑战/UVa 中
What is wrong with this C++ code? 3n+1 in programming-challenges/UVa
编程挑战网站将其标记为错误答案。我检查了样本输入,它们都是正确的。我在代码中添加了一个优化,我这样做是为了不检查已知在另一个数字序列中的数字,因为它将是一个子序列并且显然具有更短的周期长度。
此外,我刚刚回到编程中,所以程序不是太简洁,但我希望它是可读的。
这是代码:
#include <iostream>
#inclue <vector>
struct record
{
int number;
int cyclelength;
};
void GetOutput(int BEGIN, int END)
{
//determines the output order at the end of function
bool reversed = false;
if (BEGIN > END)
{
reversed = true;
int temp = BEGIN;
BEGIN = END;
END = temp;
}
vector<record> records;
for (int i = BEGIN; i <= END; ++i)
{
//record to be added to records
record r;
r.number = i;
r.cyclelength = 1;
records.push_back(r);
}
int maxCycleLength = 1;
//Determine cycle length of each number, and get the maximum cycle length
for (int i =0;i != records.size(); ++i)
{
//
record curRecord = records[i];
//ABCD: If a number is in another number's sequence, it has a lower cycle length and do not need to be calculated,
//set its cyclelength to 0 to mark that it can be skipped
if (curRecord.cyclelength != 0)
{
//
while (curRecord.number != 1)
{
//next number in the sequence
int nextNumber;
//finds the next number
if (curRecord.number % 2 == 0)
nextNumber = curRecord.number / 2;
else
{
nextNumber = curRecord.number * 3 + 1;
//if nextNumber is within bounds of input, mark that number as skippable; see ABCD
if (nextNumber <= END)
{
records[nextNumber - BEGIN].cyclelength = 0;
}
}
curRecord.number = nextNumber;
curRecord.cyclelength += 1;
}
maxCycleLength = max(curRecord.cyclelength, maxCycleLength);
}
}
if (reversed)
{
cout << END << " " << BEGIN << " " << maxCycleLength;
}
else
{
cout << BEGIN << " " << END << " " << maxCycleLength;
}
}
int main(){
//The first and last numbers
vector< vector<int> > input;
int begin, end;
while (cin >> begin >> end)
{
//storage for line of input
vector<int> i;
i.push_back(begin);
i.push_back(end);
input.push_back(i);
}
for (int i = 0;i != input.size(); ++i)
{
GetOutput(input[i][0], input[i][1]);
cout << endl;
}
return 0;
}
我会试着给你一个提示,促使你找出问题所在。
示例输入作为冒烟测试很好,但它们通常不是程序也可以处理更极端测试用例的良好指标。应始终使用超出示例输入的输入进行测试。如果我的计算正确,您的程序将为以下输入生成错误的结果:
999000 999250
作为参考,此的预期输出为:
999000 999250 321
在那里,我将搜索空间缩小到251个周期:)现在获取调试器并完成作业。
无论如何,接下来是剧透标记中的完整解释和解决方案。如果您想阅读,请将鼠标悬停在空白处,如果您想自己弄清楚,请留在原地。
<块引用类> 该问题指出 i 和 j 小于 100 万,并且没有操作溢出 32 位整数。这意味着没有中间结果大于4294967295。但是,int
是有符号类型,因此,即使它有 32 位,它也只有 31 位的绝对值,因此不能容纳任何大于 2147483647 的数字。大于这些数字出现在问题范围内几个 Ns 的周期中,其中一个是 999167。使用无符号 32 位整数是一种解决方案。
没有什么神秘的。最大的中间数溢出有符号整数的 31 位。您需要将record.number
和nextNumber
声明为 unsigned int
。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 模板元编程:如何将参数包组合成新的参数包
- Qt Q串行端口未编程设备未关闭
- 模板元编程 - 尝试实现维度分析
- 我是编程新手
- 你能帮我解决删除数组 c++ 中重复值的挑战吗?
- C++编程从外部文本文件定义数组大小
- 了解算法的性能差异(如果以不同的编程语言实现)
- 使用 Gtkmm 以编程方式选择 Gtk::TextView 中的文本
- 如何将可变参数模板转换为多个单个模板?(C++竞争编程调试模板)
- 如何改进我的代码,使其不会因超时而终止?(黑客排名挑战)
- 使用命名空间正确编程
- C++编程:运算符重载中的引用如何工作?
- Arduino 模块化编程与全局和设置
- 玩数字编程挑战(子数组的平均值)
- 我需要帮助来理解编程挑战
- 这个C++代码有什么问题?3n+1 在编程挑战/UVa 中
- 算法输出正确答案,但编程挑战给了我错误的答案
- 帮助解决编程挑战