toj 2841 Bitwise Reverse

2841.   Bitwise Reverse
Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 1520   Accepted Runs: 1025



Professor Robby invents a powerful encryption method, but he is too lazy to implement it. So he turns to you for help.

In fact, the encryption method is only applied to positive integers. At first, we express the number as binary code, that is, a string only contain '0' and '1', and the first digit can't be '0'. Then we reverse the string. And the last step, we calculate the reversed binary code and express it in decimal again.

For example, we want to encrypt the number 14, we express it as 1110, after reversing it we get 0111, and (0111)2 = 7. So we get 7.

Input

There is only one line for each test case, containing the positive integer to be encrypted. You can assume the number is not more than 106.

The input is terminated with a zero.

Output

Output one line for each test case, indicating the number after encryption.

Sample Input

5
6
14
0

 

Sample Output

5
3
7

 

Problem Setter: RoBa



Source: Tianjin Metropolitan Collegiate Programming Contest 2007


Submit   List    Runs   Forum   Statistics
                        Show Code - Run ID 633955
Submit Time: 
2009-05-09 17:10:25     Language: GNU C++     Result: Accepted
    Pid: 
2841     Time: 0.00 sec.     Memory: 1208 K.     Code Length: 0.4 K.

--------------------------------------------------------------------------------

#include 
<iostream>
#include 
<cmath>
#include 
<stack>
using namespace std;
stack
<int>S;
int main()
{
    
int n,sum;
    
while(scanf("%d",&n)!=EOF&&n!=0)
    {
        sum
=0;
        
int i=0;
        
while(!S.empty())
            S.pop();
        
while(n!=0)
        {
            
int temp=n%2;
            n
=n/2;
            S.push(temp);
        }
        
while(!S.empty())
        {
            
int temp2=S.top();
            S.pop();
            sum
+=(int)(temp2*(pow(2.0,i++)));
        }
        printf(
"%d\n",sum);
    }
    
return 0;
}

--------------------------------------------------------------------------------

Tianjin University Online Judge v1.
2.4
原文地址:https://www.cnblogs.com/forever4444/p/1453430.html