3526 - 二进制翻转
杰克被女神问的一道题难住了:输入一个非负整数(十进制),输出该数的二进制位翻转之后的数(十进制)。
他百思不得其解,机智的你能帮帮他吗?
<br />
Input
第一行一个数T(T<=100),表示数据组数,
接下来T行,每行一个非负整数a(a<=10^9),表示每一个询问。
Output
每行一个整数,表示答案。
Examples
Input
3 6 8 1
Output
3 1 1
Hint
模拟
Solution C++
#include <cstdio> #include <iostream> #include <string> #include <cstring> #include <cmath> #define for1( i , a , b ) for( int i = a ; i <= b ; i++ ) #define for2( i , a , b ) for( int i = a ; i >= b ; i-- ) using namespace std ; class Jack { public: void play() ; protected: int t , n , s , s1 , sum , w , a[ 40 ]; } ; void Jack::play() { //freopen( "in.txt" , "r" , stdin ) ; //freopen( "out.txt" , "w" , stdout ) ; cin >> t ; while( t-- ) { memset( a , 0 , sizeof( a ) ) ; cin >> n ; s = 0 ; while( n ) { a[ s++ ] = n % 2 ; n /= 2 ; } //for1( i , 0 , s ) cout << a[ i ] << ' ' ; //cout << endl ; s1 = s - 1 ; while( a[ s1 ] == 0 ) s1-- ; w = 1 , sum = 0 ; for2( i , s1 , 0 ) sum += w * a[ i ] , w <<= 1 ; printf( "%d\n" , sum ) ; } } int main() { Jack jack ; jack.play() ; return 0 ; }
Hint
模拟