Back to the Training Home
Back to the Training Home
Lesson: 39
Problem: 2841
Title: Bitwise Reverse
Difficulty Level: 1.0
Date: MAR 03, 2009
Site: http://acm.tju.edu.cn/toj/showp2841.html
Assume you know how to convert a decimal number to binary and binary to decimal number. This is good enough to solve this problem.
Solution:
#include<stdio.h>
void getResult(int n) {
int sum = 0, p = 1, index = 0;
int digit[40];
while(n) {
digit[index++] = n%2;
n /= 2;
}
for(int i = index-1; i>=0; i--) {
sum += digit[i]*p;
p *= 2;
}
printf("%d\n",sum);
}
int main() {
int n;
while(scanf("%d",&n) && n) {
getResult(n);
}
return 0;
}
|