回复:(Eastsun)楼主代码没错,但时间复杂度太高了。...
我只想到bfs.你这个先进..
[CODE]#include <iostream>
using namespace std;
int func(unsigned int x)
{
if(x <= 1) return x;
unsigned int tmp=1;
while(tmp < x)
tmp <<= 1;
int a = func( tmp-x );
int b = func( x-(tmp>>1) );
if(a < b)
return a+1;
else
return b+1;
}
int main()
{
while(1)
{
unsigned int x;
cin >> x;
unsigned int length = sizeof(unsigned int)*8-1;
if( x > ((unsigned)1<<length) )
{
cout << "too big" <<endl;
return 0;
}
if(x <= 1)
{
cout << "step 0" <<endl;
continue;
}
unsigned int tmp=1;
int step=0;
while(tmp < x)
{
tmp <<= 1;
step++;
}
int a = func(tmp - x);
int b = func(x - tmp/2);
if(a < b)
step = step + a;
else
step = step + b - 1;
cout << "step " << step << endl;
}
return 0;
}
[/CODE]