Well, this translation is very straightforward, and everyone is happy.
The first time I saw this tall question, I was a little scared. Fortunately, I have not done any in-depth articles.
As long as the rule is converted to decimal, and the question itself also says that the maximum value is no more than the range of an int (2 ^ 31-1 = 2147483647 ).
Just perform bitwise operations.
When a number is expressed in decimal,K-Th digit represents a multiple of 10K. (Digits are numbered from right to left, where the least significant digit is number 0.) For example,
When a number is expressed in binary,K-Th digit represents a multiple of 2K. For example,
InSkew binary,K-Th digit represents a multiple of 2K+ 1-1. The only possible digits are 0 and 1, cannot t that the least-significant nonzero digit can be a 2. For example,
The first 10 numbers in Skew binary are 0, 1, 2, 10, 11, 12, 20,100,101, and 102. (Skew binary is useful in some applications because it is possible to add 1 with at most one carry. however, this has nothing to do with the current problem .)
Input
The input file contains one or more lines, each of which contains an integer n. If n = 0 it signals the end of the input, and otherwise N is a nonnegative integer in Skew binary.
Output
For each number, output the decimal equivalent. The decimal value of N will be at most 231-1 = 2147483647.
Sample Input
1012020000000000000000000000000000010100000000000000000000000000000011100111110000011100001011011020000
Sample output
44214748364632147483647471041110737
Miguel A. Revilla
1998-03-10
AC code:
1 // # define local 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 using namespace STD; 6 7 char skbinary [35]; 8 9 void reverse (char s [], int L); 10 11 int main (void) 12 {13 # ifdef local14 freopen ("575in.txt", "r", stdin ); 15 # endif16 17 while (gets (skbinary) & skbinary [0]! = '0') 18 {19 int L = strlen (skbinary); 20 reverse (skbinary, L); 21 int I, n = 0; 22 for (I = 0; I <L; ++ I) 23 {24 if (skbinary [I] = '0') 25 continue; 26 if (skbinary [I] = '1 ') 27 N + = (1 <(I + 1)-1; 28 If (skbinary [I] = '2 ') 29 N + = (1 <(I + 2)-2; 30} 31 cout <n <Endl; 32} 33 return 0; 34} 35 // used to reverse the array 36 void reverse (char s [], int L) 37 {38 int I; 39 char t; 40 for (I = 0; I <l/2; ++ I) 41 {42 T = s [I]; 43 s [I] = s [L-I-1]; 44 s [L-I-1] = T; 45} 46}Code Jun