首页 > 试题广场 >

程序的输出为( )

[单选题]
#include<bits/stdc++.h>
using namespace std;
int solve(int x){
if(x == 0 || x == 1){
return x;
}
if(x % 2 == 0){
return 1 + solve(x / 2);
}
else{
return 1 + solve((x + 1) / 2);
}
}
int main(){
int n = 100;
int ans = solve(n);
cout<<ans<<endl;
return 0;
}
程序的输出为(      )
  • 6
  • 7
  • 8
  • 9
这道题实际上是求100除以2(若商为奇数,则+1再除以2),多少次才能到0或1。100/2,50/2,(25+1)/2,(13+1)/2,(7+1)/2,4/2,2/2,1,当为1的时候就退出了,所以一共是7次,加上退出时为1,所以是结果为7+1=8
编辑于 2019-09-18 10:18:38 回复(0)