Description
所谓角谷猜想,是指对于任意一个正整数,如果是奇数,则乘3加1,如果是偶数,则除以2,得到的结果再按照上述规则重复处理,最终总能够得到1。如,假定初始整数为5,计算过程分别为16、8、4、2、1。
程序要求输入一个整数,将经过处理得到1的过程输出来。
Input
一个正整数N(N <= 2,000,000)
Output
从输入整数到1的步骤,每一步为一行,每一部中描述计算过程。最后一行输出"End"。如果输入为1,直接输出"End"。
Sample Input
5
Sample Output
5*3+1=16
16/2=8
8/2=4
4/2=2
2/2=1
End
Solution
#include <cstdio>
using namespace std;
int main()
{
long long n; //int 会溢出!
scanf("%lld",&n); //%lld!
while( n > 1 ) {
if( n % 2) {
printf("%lld*3+1=%lld\n",n,n * 3 + 1);
n = n * 3 + 1;
}
else {
printf("%lld/2=%lld\n",n,n / 2);
n = n / 2;
}
}
printf("End\n");
return 0;
}