ACM_Hailstone HOTPO

Hailstone HOTPO

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

The hailstone sequence is formed in the following way:
(1) If n is even, divide it by 2 to get n'
(2) If n is odd, multiply it by 3 and add 1 to get n'
It is conjectured that for any positive integer number n, the sequence will always end in the repeating cycle: 4, 2, 1, 4, 2, 1,... Suffice to say , when n == 1, we will say the sequence has ended.
Write a program to determine the largest value in the sequence for a given n. 
Translation: Form the hail sequence in the following way: (1) If n is even, divide by 2 to get N" (2) If n is odd, Multiply by 3, and add 1 to get N" Presumably, for any positive integer n, the sequence will always end with a repeating period: 4, 2, 1, 4, 2, 1, ... as long as n = = 1, we would say the sequence has ended. Write a program to determine the maximum value in a sequence given n.

Input:

The first line of input contains a single integer P, (1<= P <= 100000), which is the number of data set s that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input consisting of two space separated decimal integers. The first integer is the data set number. The second integer is n, (1 <= n <= 100,000), which is the starting value 
. : The first line of input contains an integer P, (1 <= P <= 100000), which is the number of datasets that follow. Each dataset should be processed identically and independently. Each dataset consists of a single-line input consisting of two space-separated decimal integers. The first integer is the dataset number. The second integer is n, (1 <= n <= 100,000), which is the starting value.

Output:

For each data set there is a single line of output consisting of the data set number, a single space, and the largest value in the sequence starting at and including 
n . The dataset number, a single space, and the maximum value in the sequence starting at n.

Sample Input:

4
1 1
2 3
3 9999
4 100000

Sample Output:

1 1
2 16
3 101248
4 100000 
problem-solving ideas: The problem is very simple, that is, to find the maximum value in the sequence, and find it according to the rules. We know that the sequence is either odd or even, so we judge whether it is an odd number in the loop. If so, it becomes an even number after executing rule (2), so it is easy to think that n/=2 is directly executed in the loop body at the end; plus the search The judgment statement of the maximum value in the sequence, the water has passed.
AC code:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int p,m,n,maxn;
 6     cin>>p;
 7     while(p--){
 8         cin>>m>>n;
 9         maxn=n;
10         while(n!=1){
11             if(n%2)n=n*3+1;
12             if(maxn<n)maxn=n;
13             n/=2;
14         }
15         cout<<m<<' '<<maxn<<endl;
16     }
17     return 0;
18 }

Hangdian hdu4484 is the same as this question, the title link: http://acm.hdu.edu.cn/showproblem.php?pid=4484

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559817&siteId=291194637