HDU 4484 Hailstone HOTPO (简单模拟题)

Hailstone HOTPO

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 516    Accepted Submission(s): 306



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.
 

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.
 

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.
 

Sample Input
  
  
4 1 1 2 3 3 9999 4 100000
 

Sample Output
  
  
1 1 2 16 3 101248 4 100000
 

Source
 

Recommend
liuyiding


题意:
给你一个数字n,如果是奇数就将它乘以3加上1,如果是偶数就将它除以2,直到这个数变为1为止,问:在这个变化过程中,n变到过的最大的数(包括原先的n值)。
思路:
直接按题意变化该数,记录最大的那个数即可。

/*************************************************************************
	> File Name: AA.cpp
	> Author: BSlin
	> Mail:  
	> Created Time: 2013年10月06日 星期日 12时29分21秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-8
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif



int main(int argc, char** argv) {
    int t,num,max,n,temp;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&num,&n);
        max = n;
        while(n != 1) {
            if(n % 2) {
                n = n * 3 + 1;
            } else  {
                n >>= 1;
            }
            if(max < n) max = n;
        }
        printf("%d %d\n",num,max);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值