3n+1问题 总是time limit exceeded

问题描述:

3n+1问题 总是time limit exceeded
如题.具体问题是,随便给一个数n,如果n是偶数,那么除以2,如果n是奇数,那么3*n+1,这么循环下去,直到达到1为止.当中经过的步骤数量称作cycle length,比如说,8->4->2->1,这个的cycle length就是4.
现在随便给两个数 i 和 j,要求出 i 与 j 当中所有数(包括 i 和 j )的最大的cycle length,然后print出来.例子如下,
Sample Input
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
我在我自己的电脑上test我的代码,test包括最极端的情况,input是1 999999的情况,运行时间绝对不到1秒.时间要求是3秒以下,但是为什么每次我提交给uvaonlinejudge,它总说我time limit exceeded?
我的代码如下:
#include
#include
#include
#include
#include
#include
using namespace std;
vector array(1000000);
int getcycle(int x)
{
int count=0;
int orgin=x;
while (x = 1) {
if ((x < 1000000) && (array[x] = NULL)) {
count += array[x];
break;
}
else {
float temp = (float)x/2;
float diff = temp - floor(temp);
if (diff==0) {
\x05count++;
\x05x = x/2;
}
else {
\x05count++;
\x05x = 3*x + 1;
}
}
}
array[orgin]=count;
return count;
}
int findmax(int x,int y)
{
int max=0;
int bigger;
int smaller;
if (x
1个回答 分类:综合 2014-12-06

问题解答:

我来补答
#include
int main(void){
int i,j;
while (scanf("%d%d",&i,&j)!=EOF){
int a,b,max=0;
i
 
 
展开全文阅读
剩余:2000