描述 Description
奶牛们又在玩一种无聊的数字游戏。输得很郁闷的贝茜想请你写个程序来帮她在开局时预测结果。在游戏的开始,每头牛都会得到一个数 N(1<=N<=1,000,000)。此时奶牛们的分数均为 0。如果 N 是奇数,那么奶牛就会把它乘以 3 后再加 1。如果 N 是偶数,那么这个数就会被除以 2。数字每变动一次,这头奶牛就得到 1 分。当 N 的值等于 1 时,游戏结束,此时的分数就是这头奶牛在这局游戏中的最终得分。 以下是 N 的初始值为 5 时,一局游戏的完整过程: N 操作后所得数 注释 总分 5 16 3*5+1 1 16 8 16/2 2 8 4 8/2 3 4 2 4/2 4 2 1 2/2 5 这头奶牛的最终得分是 5。
输入格式 InputFormat
第 1 行: 一个正整数,N
输出格式 OutputFormat
第 1 行: 输出一个正整数 N,即奶牛在这局游戏中的最终得分
样例输入 SampleInput
156159
样例输出 SampleOutput
382
来源 Source
Gold
代码 Code
水。
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x7fffffff;
long long i,j,t,n,m,l,r,k,z,y,x,ans;
int main()
{
scanf("%d",&n);
ans=0;
while (n!=1)
{
if (n%2==0) n/=2;
else n=n*3+1;
ans++;
}
printf("%d\n",ans);
return 0;
}