The 3n + 1 problem UVA - 100

3n+1问题

PC/UVa IDs: 110101/100

Popularity: A

Success rate: low Level: 1

测试地址:

https://vjudge.net/problem/UVA-100

[问题描述]

考虑如下的序列生成算法:从整数 n 开始,如果 n 是偶数,把它除以 2;如果 n 是奇数,把它乘 3 加1。用新得到的值重复上述步骤,直到 n = 1 时停止。例如,n = 22 时该算法生成的序列是:

22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1

人们猜想(没有得到证明)对于任意整数 n,该算法总能终止于 n = 1。这个猜想对于至少 1 000 000内的整数都是正确的。

对于给定的 n,该序列的元素(包括 1)个数被称为 n 的循环节长度。在上述例子中,22 的循环节长度为 16。输入两个数 i 和 j,你的任务是计算 i 到 j(包含 i 和 j)之间的整数中,循环节长度的最大值。

[输入]

输入每行包含两个整数 i 和 j。所有整数大于 0,小于 1 000 000。

[输出]

对于每对整数 i 和 j,按原来的顺序输出 i 和 j,然后输出二者之间的整数中的最大循环节长度。这三个整数应该用单个空格隔开,且在同一行输出。对于读入的每一组数据,在输出中应位于单独的一行。

[样例输入]

1 10

100 200

201 210

900 1000

[样例输出]

1 10 20

100 200 125

201 210 89

900 1000 174

 1 #include<iostream>
 2  using namespace std;
 3
 4   int main()
 5 {
 6 int  k,j;
 7 while(cin>>k>>j)
 8 {    int tem;
 9     int e=0;
10 if(k>j)//判断kj大小,如果发生调换,e=1
11     {tem=j;
12     j=k;
13     k=tem;
14     e=1;
15     }
16 int t=0,max=0;
17     for(int  i=k;i<=j;i++)
18     {
19         int n=i;
20     while(1)
21     {
22         if(n==1)
23         {t++;break;
24         }
25         if(n&1==1)//qi
26         {n=3*n+1;
27         }
28         else n/=2;
29         t++;
30
31     }
32
33     if(t>max)
34     {max=t;
35     }
36     t=0;
37     }
38 if(e==1)//发生调换后,输出是调换前的值
39 cout<<j<<" "<<k<<" "<<max<<endl;
40 else
41 cout<<k<<" "<<j<<" "<<max<<endl;
42
43 }
44 return 0;
45 }

测试没问题,但是提交后出错;看了别人的博客后发现问题所在,题目没有规定输入两个数一定i<j;即可以输入10  1;

原文地址:https://www.cnblogs.com/niliuxiaocheng/p/10548019.html

时间: 2024-08-30 14:29:44

The 3n + 1 problem UVA - 100的相关文章

UVA 100 The 3n + 1 problem(超级大水题)

The 3n + 1 problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you

100 - The 3n + 1 problem

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36  The 3n + 1 problem  Background Problems in Computer Science are often classified as belonging to a certain class of problem

UVA 100 The 3*n+1 problem

  UVA 100 The 3*n+1 problem. 解题思路:对给定的边界m,n(m<n&&0<m,n<1 000 000);求X(m-1<X<n+1)进过操作至1的最大步数. 对m到n依次计算,比较获得最大步数. 注意:1.输入格式. 本题利用  while(scanf("%d %d",&m,&n)!=EOF)                  {                 }       2.输出与题给出相同

POJ1207 The 3n + 1 problem

这题不是很难,模拟一下即可,但有一些细节需要注意 输入的数不一定升序,且要按原顺序输出,还有就是读完数据的问题 The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53006   Accepted: 16841 Description Problems in Computer Science are often classified as belonging to a certain cl

The 3n + 1 problem(杭电1032)(暴力求解)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23163    Accepted Submission(s): 8653 Problem Description Problems in Computer Science are often classified as belonging to a

HDU 1032 The 3n + 1 problem【递归】

/* 中文题目 3n+1问题 中文翻译-大意 当一个数n是奇数的时候变成3*n+1,为偶数的时候变成n/2 解题思路:区间内每个数逐个求解 难点详解:如何逐个计算每个数的次数,选用while循环,还有就是将此时的 i 值赋给data,用于while循环的条件.最后再将这一个数运算次数累加在一起 关键点:理解题意 解题人:lingnichong 解题时间:2014-06-03 09:40:39 解题体会:没有告诉你那个大那个小,要先比较一下数,是个陷阱 */ The 3n + 1 problem

HDU 1032 The 3n + 1 problem (这个题必须写博客)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22148    Accepted Submission(s): 8276 Problem Description Problems in Computer Science are often classified as belonging to a c

hdu 1032 The 3n + 1 problem (打表)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26353    Accepted Submission(s): 9784 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 Problem Description Problems in Comp

The 3n + 1 problem

The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50648   Accepted: 16059 Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In th