🥇C/C++语言100题练习专栏计划:目的:巩固练习C/C++语言,增强上机、动手实践能力,交流学习!
一、问题呈现
1.问题描述
Problem Description
从前有—个数字1,它每天要么+1,要么*2现在它变成了N,请问它最少用了几天变成N。
2.输入输出
Input
输入只有一行N,代表最终的数字
Output
输出只有一行表示答案
3.测试样例
样例1
Sample Input
2
Sample Output
1
样例2
Sample Input
6
Sample Output
3
二、源码实现
#include<iostream>
using namespace std;
int main()
{
int flag;
int n;
while(cin>>n)
{
flag=0;
while(n!=1)
{
if(n%2!=0)
{
n--;
flag++;
}
else
{
n=n/2;
flag++;
}
}
cout<<flag<<endl;
}
return 0;
}
三、测试结果
6
3
^Z
--------------------------------
Process exited after 2.68 seconds with return value 0
请按任意键继续. . .