当前位置: > 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;
}

提问时间:2021-03-31

答案
#include
int main(void){
int i,j;
while (scanf("%d%d",&i,&j)!=EOF){
int a,b,max=0;
i
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
版权所有 CopyRight © 2012-2019 超级试练试题库 All Rights Reserved.