3n+1 uVa gives WA
3n+1 uVa gives WA
我最初的问题有点模糊,所以这里是编辑后的问题。
3n+ 1uva问题基于Collatz猜想。考虑任意正整数n。如果是偶数,把它除以2。如果是奇数,把它乘以3再加1。经过x次这样的重复运算,你会得到1。这已经被超级计算机证明了。
UVa Online Judge是由巴利亚多利德大学主办的编程问题在线自动裁判。
下面是问题说明的链接:uVa 3 n + 1
请在查看我的代码之前阅读问题声明!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;
}
下面是我对以下输入得到的输出:
输入:1 1
10 1 201年210
113383年113383
999999 1
输出:1 1 1
10 1 20
210 201 89
113383 113383 248
999999 1 525
另一个testcase:
输入:5
110 8
202年210
113383年113383
999989年999999
输出:5
20
210 202 89
113383 113383 248
999999 999989 259
最后一个被检查的数字的周期长度不会与最大值进行比较。
输入:8 9
9 9
程序输出
8 9 4
9 9 20
但正确答案是
8 9 20
9 9 20
把
if (cycles > maxcycles) {
maxcycles = cycles;
}
放在for循环的末尾,而不是开头。
相关文章:
- 为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
- 我已经通过了该问题的所有测试用例,甚至是Udebugg上的所有测试用例,并且仍然在UVA在线法官上获得了WA裁决
- 带地图的解决方案给了空调,unordered_map的解决方案给了WA.为什么
- 为什么我在SPOJ上为Buglife获得WA
- C: strtok value gives back null
- cudaGraphicsGLRegisterImage gives cudaErrorMemoryAllocation
- SPOJ GSS1 WA - Segment tree
- 当我试图提交mkbudge-spoj时获得WA
- glDrawElements gives EXC_BAD_ACCESS with VBO
- Getting WA for SPOJ smpseq
- 给出WA的简单算术(ARITH)
- 为什么我在Codechef的褪色回文中得到一个WA,即使我在我的代码中没有发现任何错误,它对我来说工作得很好
- 3n+1 uVa gives WA
- C++: string = NULL gives SEGFAULT 11?
- 从Codechef长征比赛中获得ANUGCD的WA
- 在SPOJ INCSEQ -递增子序列中获取WA