题目
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;
}
如题.具体问题是,随便给一个数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
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的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
- 1英语打电话时Who is this?与Who is that有什么区别
- 2小学五年级下册苏教版的语文练习册第3课第四大题的第3小题的答案急
- 3把一个长方体的宽增加2厘米后,就变成一个棱长是10厘米的正方体,原来长方体的体积是_立方厘米.
- 4knock out drum的中文意思是什么
- 5200×80%+χ=(200+χ)×20%
- 6有浓度为30%的溶液若干,加了一定数量的水后稀释成24%的溶液,如果再加入同样多的水后,浓度将变为多少?
- 7与S有关的化学方程式都有什么
- 8They each have a watch(改同义句)_______ _________ ________has a watch.
- 9你叫什么名字英语怎么说?
- 10Here are the results of the (.) activity survey at Hilltop High School.(student) 填什么,为什么
热门考点