zoukankan      html  css  js  c++  java
  • GDUFE ACM-1006

    题目:http://acm.gdufe.edu.cn/Problem/read/id/1006

    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.

    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

    思路:判断是基数还是偶数,然后按题目要求执行,判断执行后的数与执行前的数哪个比较大,把大的留下来,直到得出1,结束

    难度:简单

    代码:
     1 #include<stdio.h>
     2 int main()
     3 {
     4     int n,a,b,c;
     5     while(scanf("%d",&n)!=EOF)
     6     {
     7         while(n--)
     8         {
     9             scanf("%d %d",&a,&b);
    10             c=0;
    11             if(b==1) c=1;
    12             while(b!=1)
    13             {
    14                 if(b>c)
    15                     c=b;
    16                 if(b%2==0)
    17                     b=b/2;
    18                 else b=b*3+1;
    19             }
    20             printf("%d %d
    ",a,c);
    21         }
    22     }
    23     return 0;
    24 }
  • 相关阅读:
    A1052. Linked List Sorting (25)
    A1032. Sharing (25)
    A1022. Digital Library (30)
    A1071. Speech Patterns (25)
    A1054. The Dominant Color (20)
    A1060. Are They Equal (25)
    A1063. Set Similarity (25)
    电子码表
    矩阵键盘
    对象追踪、临时对象追踪、绝对坐标与相对坐标
  • 原文地址:https://www.cnblogs.com/ruo786828164/p/6005111.html
Copyright © 2011-2022 走看看