PAT Class B true question 1001. The dead are not worth the candle (3n + 1) conjecture (15) (Problem Solving), pat3n

Source: Internet
Author: User

PAT Class B true question 1001. The dead are not worth the candle (3n + 1) conjecture (15) (Problem Solving), pat3n

Callatz conjecture:

For any natural number n, if it is an even number, cut it by half; if it is an odd number, cut (3n + 1) by half. In this way, we will continue to cut down and finally get n = 1 in a certain step. Karaz announced this conjecture at the world mathematician conference in 1950. It is said that at that time, the teachers and students of Yale University were mobilized to prove this seemingly silly and naive proposition, and the results made the students unwilling to study, as a result, some people say this is a conspiracy. karaz is deliberately delaying the progress of teaching and scientific research in American mathematics ......

Today's question is not to prove caraz's conjecture, but to give a given positive integer n of no more than 1000, simply count it. How many steps (cut down) Do we need to get n = 1?

Input Format: Each test input contains one test case, that is, the value of natural number n. Output Format: Number of steps required for the output from n to 1. Input example: 3 Output example: 5

Code:

 

1 # include <stdio. h> 2 int main () 3 {4 int n = 0; // receives the n of the pre-judgment; 5 int I = 0; // saves the number of calculated steps; 6 scanf ("% d", & n); 7 while (n! = 1) // if 1 is not obtained, it is cut all the time; 8 {9 if (n % 2 = 0) 10 {11 n/= 2; 12} 13 else14 {15 n = (3 * n + 1)/2; 16} 17 I ++; 18} 19 printf ("% d", I ); 20 return 0; 21}
/* Compile the environment ********** vc ++ 6.0 (32-bit )*************/

 

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.