标签:
http://acm.hdu.edu.cn/showproblem.php?pid=5142
A boy named NPY is learning FFT algorithm now.In that algorithm,he needs to do an operation called "reverse".
For example,if the given number is 10.Its binary representaion is 1010.After reversing,the binary number will be 0101.And then we should ignore the leading zero.Then the number we get will be 5,whose binary representaion is 101.
NPY is very interested in this operation.For every given number,he want to know what number he will get after reversing.Can you help him?
The first line contains a integer T — the number of queries $(1 \leq T \leq 100).$
The next T lines,each contains a integer $\begin{align*}X(0 \leq X \leq 2^{31}-1)\end{align*}$,the given number.
For each query,print the reversed number in a separate line.
3
6
8
1
3
1
1
1 #include<cstring> 2 #include<cstdio> 3 #include<cmath> 4 int buf[40]; 5 void go(int n) { 6 int i, j = 0, res = 0; 7 memset(buf, 0, sizeof(buf)); 8 do buf[j++] = n % 2; while (n /= 2); 9 for (i = j - 1; ~i; i--) res += buf[i] * (int)pow(2, j - i - 1); 10 printf("%d\n", res); 11 } 12 int main() { 13 int t, n; 14 scanf("%d", &t); 15 while (t--) scanf("%d", &n), go(n); 16 return 0; 17 }
标签:
原文地址:http://www.cnblogs.com/GadyPu/p/4561594.html