C++ 3n+1 uVa产生WA

C++ 3n+1 uVa产生WA,c++,collatz,C++,Collatz,我最初的问题有点模糊,所以这里是经过编辑的问题 3n+1uva问题基于Collatz猜想。 考虑任何正整数“n”。如果是偶数,则将其除以2。如果是奇数,则将其乘以3并加1。在“x”这样的重复操作之后,您将得到1。超级计算机已经证明了这一点 UVA在线评委是巴利亚多利德大学主办的在线编程问题自动评判员。 以下是问题陈述的链接: 在查看我的代码之前,请先阅读问题说明! uVa在线法官针对某些测试用例运行您的代码,并告诉您解决方案是对还是错。如果您的解决方案错误,它不会告诉您为什么失败 如果您错过了

我最初的问题有点模糊,所以这里是经过编辑的问题

3n+1uva问题基于Collatz猜想。 考虑任何正整数“n”。如果是偶数,则将其除以2。如果是奇数,则将其乘以3并加1。在“x”这样的重复操作之后,您将得到1。超级计算机已经证明了这一点

UVA在线评委是巴利亚多利德大学主办的在线编程问题自动评判员。

以下是问题陈述的链接:

在查看我的代码之前,请先阅读问题说明! uVa在线法官针对某些测试用例运行您的代码,并告诉您解决方案是对还是错。如果您的解决方案错误,它不会告诉您为什么失败

如果您错过了第一个链接,请再次访问此链接:

我不明白我遗漏了什么或跳过了什么,因为我在代码逻辑中得到了错误的答案。我已经尝试了很多测试用例,它们似乎工作得很好。我知道逻辑可以被压缩到很大程度,但我现在只需要找出缺陷所在

这是我的密码。我知道bits/stdc++不应该被使用,但目前它并不影响我的代码。如果可以的话,请检查一下并帮助我

#include<iostream>
#include<cstdlib>
#include<bits/stdc++.h>
using namespace std;

int main()
{
unsigned long int num;
int i,j,tempi, tempj,temp;

//Scanning until inputs don't stop
while(scanf("%d %d",&i,&j)!=EOF)
{
//Boolean variable to set if i is greater
bool iwasmore=false;
//Boolean variable for equal numbers
bool equalnums=false;
int cycles=1, maxcycles=0;

if(i>j)
{
//swapping for the for loop to follow
temp=i; i=j; j=temp;
iwasmore=true;
}

if(i==j)
{
    equalnums=true;
}

tempi=i; tempj=j;   

//Taking each number in the given range and running it in the algorithm.
//The maxcycles variable will have the value of the maximum number of cycles
//that one of the numbers in the range took (at the end of the for loop).
for(num=i;num<=j;num=(++tempi))
{
if(cycles>maxcycles)
{
    maxcycles=cycles;
}
cycles=1;
//The actual algorithm
while(num!=1)
    {
        if(num%2==1)
        {
        num = (3*num)+1;
        cycles++;
        }

        else
        {
        num=(num/2);
        cycles++;
        }
    }

    if(equalnums==true)
        {
            maxcycles=cycles;
            equalnums=false;
        }
//Resetting num
    num=0;
}
if(!iwasmore)
cout<<i<<" "<<j<<" "<<maxcycles<<endl;
else
cout<<j<<" "<<i<<" "<<maxcycles<<endl;
}
return 0;
}
以下是我为以下输入获得的输出:

输入:

11

101

210 201

113383 113383

999991

输出:

11

10120

210 201 89

113383 113383 248

999991525

另一个测试用例:

输入:

15

108

210 202

113383 113383

999999999989

输出:

1 5 8

10 8 20

210 202 89

113383 113383 248


999999999989 259

最后检查的数字的周期长度永远不会与最大值进行比较

输入:

8 9
9 9
程序输出

8 9 4
9 9 20
但正确的答案是

8 9 20
9 9 20


在for循环的末尾,而不是开始。

您缺少或跳过了正确的问题描述。什么是uVa?什么是WA?你想干什么?什么不起作用?喂。我给了一个问题描述的链接,在这里复制粘贴太长了。如果输入是11,你的程序会输出什么?@SaunvedMutalik不希望链接或复制粘贴,但是你的描述。顺便说一句,包含是错误的。我很确定我在其他程序中也犯了这个错误!终于找到了解决办法。非常感谢。代码现在被接受了:
if (cycles > maxcycles) {
    maxcycles = cycles;
}