这个C++代码有什么问题?3n+1 在编程挑战/UVa 中

What is wrong with this C++ code? 3n+1 in programming-challenges/UVa

本文关键字:编程 挑战 UVa 3n+1 代码 C++ 什么 问题 这个      更新时间:2023-10-16

编程挑战网站将其标记为错误答案。我检查了样本输入,它们都是正确的。我在代码中添加了一个优化,我这样做是为了不检查已知在另一个数字序列中的数字,因为它将是一个子序列并且显然具有更短的周期长度。

此外,我刚刚回到编程中,所以程序不是太简洁,但我希望它是可读的。

这是代码:

#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.numbernextNumber声明为 unsigned int