HDU——1032 The 3n + 1 problem
2015-09-09 20:43
330 查看
B - The 3n + 1 problem
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
1032
Appoint description:
eycia (2015-01-13)System Crawler (2015-09-05)
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 will be analyzing a property of an algorithm whose classification is not known for
all possible inputs.
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then n <- 3n + 1
5. else n <- n / 2
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that
0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
You can assume that no opperation overflows a 32-bit integer.
Output
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line
and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
Sample Input
Sample Output
注意输入的两个数大小不确定,需要比较
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<float.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<vector>
#include<numeric>
#include<bitset>
#include<queue>
#include<stack>
#define M(i,n,m) for(int i=n;i<=m;i++)
#define N(j,n,m) for(int j=n;j>=0;j--)
#define L(N,n) memset(N,n,sizeof(N))
const int MAX=1<<30;
const int MIN=-MAX;
using namespace std;
double PI=acos(-1);///用C输出
int k;
void s(int x)
{
if(x==1)
return;
if(x%2!=0)
{
k++;
s(x*3+1);
}
else
{
k++;
s(x/2);
}
}
int main()
{
int n,m,t;
while(~scanf("%d%d",&n,&m))
{
printf("%d %d ",n,m); ///防止比较后n和m的位置发生变化,影响最后的输出结果
if(n>m)
{
int t=n;
n=m;
m=t;
}
t=MIN;
M(i,n,m)
{
k=1;
s(i);
t=max(t,k);
}
printf("%d\n",t);
}
return 0;
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
1032
Appoint description:
eycia (2015-01-13)System Crawler (2015-09-05)
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 will be analyzing a property of an algorithm whose classification is not known for
all possible inputs.
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then n <- 3n + 1
5. else n <- n / 2
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that
0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
You can assume that no opperation overflows a 32-bit integer.
Output
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line
and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
Sample Input
1 10 100 200 201 210 900 1000
Sample Output
1 10 20 100 200 125 201 210 89 900 1000 174
注意输入的两个数大小不确定,需要比较
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<float.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<vector>
#include<numeric>
#include<bitset>
#include<queue>
#include<stack>
#define M(i,n,m) for(int i=n;i<=m;i++)
#define N(j,n,m) for(int j=n;j>=0;j--)
#define L(N,n) memset(N,n,sizeof(N))
const int MAX=1<<30;
const int MIN=-MAX;
using namespace std;
double PI=acos(-1);///用C输出
int k;
void s(int x)
{
if(x==1)
return;
if(x%2!=0)
{
k++;
s(x*3+1);
}
else
{
k++;
s(x/2);
}
}
int main()
{
int n,m,t;
while(~scanf("%d%d",&n,&m))
{
printf("%d %d ",n,m); ///防止比较后n和m的位置发生变化,影响最后的输出结果
if(n>m)
{
int t=n;
n=m;
m=t;
}
t=MIN;
M(i,n,m)
{
k=1;
s(i);
t=max(t,k);
}
printf("%d\n",t);
}
return 0;
}
相关文章推荐
- Eclipse已经安装了SVN插件,但是在获取SVN代码时,一直处于progress....
- 框架数据连接类
- mysql 备份脚本
- hdu3516 Tree Construction
- android待机添加电量小部件,点击位置信息图标,模式切换需要改为四种
- effective c++若不想使用编译器自动生成的函数,就应该明确拒绝
- 【转】java格式化输出 printf 例子
- ViewDragHelper完全解析以及对自定义ViewGroup的作用
- ORACLE TO_CHAR函数详解及其他格式转换说明
- 史上最著名的10个思想实验(思想解放运动)
- source insight 调用 vim
- 《剑指Offer》面试题:翻转单词顺序
- dnsmasq+postfix+sasl+tls+dovecot(邮件服务器)
- Rime 协议栈地址 rimeaddr_t
- Android Service——在子线程中更新UI
- 十道海量数据处理面试题与十个方法大总结
- 数据结构--单向链表(c++)
- div嵌套时背景图片不显示问题解决方法总结
- 设计模式之原型模式
- java 排序算法-选择排序