C/C++ Programming Learning-Week 7 ③ Kakutani Conjecture

Topic link

Title description

Mr. Suantou: The so-called Kakutani conjecture means that for any positive integer, if it is an odd number, multiply it by 3 and add 1, if it is an even number, divide it by 2, and the result will be repeated according to the above rules, and finally 1 will always be obtained. For example, assuming that the initial integer is 5, the calculation process is 16, 8, 4, 2, and 1, respectively.

The program requires the input of an integer, and outputs the process that has been processed to get 1.

Input format
A positive integer N (N≤2,000,000).

Output format
The steps from the input of an integer to 1, each step is one line, and each section describes the calculation process. The last line outputs "End". If the input is 1, directly output "End".

Data guarantee that midway calculation will not exceed the range of int.

Sample Input

5

Sample Output

5*3+1=16
16/2=8
8/2=4
4/2=2
2/2=1
End

Ideas

Kakutani conjecture means that for any positive integer, if it is an odd number, multiply it by 3 and add 1, if it is an even number, divide it by 2, and the result will be repeated according to the above rules, and finally 1 will always be obtained.

So we can simulate this conjecture, do 1 step by step, and finally output End. The specific step is to output the unchanged x first, and then make a corresponding change to x to obtain a new x and output. Bit operations will be faster.

C language code:

#include <stdio.h>
int main()
{
    
    
	long long x;
	scanf("%ld", &x);
	while(x != 1)
	{
    
    
		if(x % 2 != 0)
		{
    
    
			printf("%ld",x);	//先输出没有变化的x
			x = x * 3 + 1;		//对x做题目要求的变化
			printf("*3+1=%d\n", x);	//得到新的x了
		}
		else
		{
    
    
			printf("%ld", x);
			x /= 2;
			printf("/2=%ld\n", x);
		}
	}
	if(x == 1) printf("End");
	return 0;
}

C++ code:

#include<bits/stdc++.h>
using namespace std;
int n, flag;
void solve()
{
    
    
	if(n == 1)
	{
    
    
		cout << "End" << endl;
		flag = 0;
	}
	else
	{
    
    
		if(n & 1)
		{
    
    
			cout << n << "*3+1=";
			n = n * 3 + 1;
			cout << n << endl;
		}
		else
		{
    
    
			cout << n << "/2=";
			n /= 2;
			cout << n << endl;
		}
	}
}
int main()
{
    
    
	while(cin >> n)
	{
    
    
		flag = 1;
		while(flag)
			solve();
	}
	return 0;
}

For students who don’t have C language foundation, you can learn the C language grammar first. I will sort it out and send it out later.
I have already written it. You can go to the C language programming column to see the content of the first week .

Other exercises this week:

C language programming column

C/C++ Programming Learning-Week 7① Calculate the value of (a+b)*c

C/C++ Programming Learning-Week 7② Calculate the value of (a+b)/c

C/C++ Programming Learning-Week 7 ③ Kakutani Conjecture

C/C++ programming learning-week 7④ cocktail therapy

C/C++ Programming Learning-Week 7 ⑤ The number of the same number as the specified number

C/C++ Programming Learning-Week 7 ⑥ Group photo effect

C/C++ Programming Learning-Week 7⑦ Word Flip

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112912148