Shandong University of Technology ACM platform answers questions about C language 1601 3n + 1 data chain

Source: Internet
Author: User

3n + 1 data chain Time Limit: 1000 ms Memory limit: 65536 K any questions? Click Here ^_^ In computer science, there are many similar problems that cannot be solved. However, in many cases, we do not know which type of problem can be solved, and which type of problem cannot be solved. Now we have the following problem:
(1) enter a positive integer n;
(2) display n;
(3) end if n = 1;
(4) If n is an odd number, then n becomes 3 * n + 1, otherwise it becomes n/2;
(5) Transfer to step (2.
For example, for the input positive integer 22, the following series should be displayed:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
We speculate that for any positive integer, after the above algorithm, it will eventually be pushed to 1. Although this algorithm is simple, we still cannot determine whether our inference is correct. Fortunately, we have a computer and we have verified that all positive integers smaller than 1000000 satisfy the above inference.
For a given positive integer n, we define the number of displayed numbers as the chain length of n. For example, the chain length of 22 is 16.
Your task is to write a program. For any positive integer I and j, the maximum chain length between I and j is given, of course, the longest chain length is generated by a positive integer between I and j. Here I and j include I and j. Input two integers I, j, I, and j are separated by a space. 0 <I <= j <10000. The output data has only one row, that is, the longest chain length between I and j. Sample Input
1 10
Sample output
20
# Include
Int getLinkLen (int n)
{
If (n = 1) return 1;
Return n & 1? GetLinkLen (n * 3 + 1) + 1: getLinkLen (n/2) + 1;
}
Int main ()
{
Int x, y, I, t, max = 0;
Scanf ("% d", & x, & y );
For (I = x; I <= y; I ++ ){
T = getLinkLen (I );
If (t> max) max = t;
}
Printf ("% d \ n", max );
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.