PAT1001害死人不偿命的(3n+1)猜想(C++)

PAT1001害死人不偿命的(3n+1)猜想(C++)

PAT1001害死人不偿命的(3n+1)猜想(C++)_第1张图片
这道题主要用到了循环语句和条件判断语句,比较简单,适合新手入门。

C++代码如下:

#include
using namespace std;

int main()
{
     
	int n;
	cin >> n; int i = 0;
	while (n!=1)
	{
     
		if (n % 2 == 0)
		{
     
			n = n / 2;
		}
		else
		{
     
			n = (3 * n + 1) / 2;
		}
		i++;
	}
	cout << i;
	return 0;
}

你可能感兴趣的:(C++编程题,C++编程实例)