1001 Thoughts of killing people without paying their lives (3n+1) conjecture

                                     1001 害死人不偿命的(3n+1)猜想 (15分)    

Callatz conjecture:

For any positive integer n, if it is even, cut it in half; if it is odd, cut (3n+1) in half. This has been repeated repeatedly, and finally n=1 must be obtained at a certain step. Karaz announced this conjecture at the World Congress of Mathematicians in 1950. According to legend, teachers and students of Yale University were mobilized at that time, desperately trying to prove this seemingly silly and naive proposition. As a result, the students were indifferent to their studies. +1), so much so that some people say this is a conspiracy, Karaz is deliberately delaying the progress of teaching and scientific research in American mathematics...

Our topic today is not to prove Karaz’s conjecture, but to simply count any given positive integer n not exceeding 1000. How many steps (cut several times) does it take to get n=1?
Input format:
Each test input contains 1 test case, which gives the value of a positive integer n.

Output format:
output the number of steps required to calculate from n to 1.

Input sample:
3
Output sample:
5

code

#include<stdio.h>
int main()
{
    int n;
    int step=0;
    scanf("%d",&n);
    while(n!=1)
    {
        if(n%2==0)
            n=n/2;
        else
            n=(3*n+1)/2;
          step++;
    }
      printf("%d\n",step);
    return 0;
}

Run screenshot
1001 Thoughts of killing people without paying their lives (3n+1) conjecture
This run result is the test result on the official website of Zhejiang University PAT The
test environment is gcc 6.5.0

The code in Visual Studio is different from the code in the test, and it can be run, but it cannot be run on the official website of the PAT test, and there is an error in the test point.
Below is the code in Visual Studio 2019

#include<stdio.h>
int main()
{
    int n;
    int step = 0;
    scanf("%d", &n);
    while (n != 1)
    {
            if (n % 2 == 0)
            {
                n = n / 2;
            }
            else
            {
                n = (3 * n + 1) / 2;
            }
        step++;
    }
    printf("step=%d\n", step);
    return 0;
}


1001 Thoughts of killing people without paying their lives (3n+1) conjecture
Although the running result can be compiled in PAT, the score is 0.
1001 Thoughts of killing people without paying their lives (3n+1) conjecture
The warning here is to remind you that you did not use the return value of scanf.
It is not difficult to see that the two codes are only different in the output function of printf
but the second one. The code cannot be compiled in PAT's gcc 6.5.0 compiler.
I saw in other people’s blogs that

warning不会对程序本身逻辑造成影响,忽略即可。
程序未通过测试,要检查代码本身的错误

But I haven't found the logical error of this code.

希望懂得的人不吝赐教

The second is the test points in PAT. It
seems that the test points in PAT will not be given to you. If the test points are given to you, everyone should get full marks.
The above is my experience and experience of PAT Level B 1001 program questions. The initial questions are relatively simple.
I will continue to update the remaining topics, hoping to write good code.

Guess you like

Origin blog.51cto.com/14950896/2542371