UVa 575 / ZOJ 1712 / Mid-Central USA 1997 Skew Binary2014-04-26 csdn博客 synapse7UVa 575 / ZOJ 1712 / Mid-Central USA 1997 Skew Binary (water ver.&斜二进制)575 - Skew BinaryTime limit: 3.000 secondshttp://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=516http://poj.org/problem?id=1362http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1712When a number is expressed in decimal, the
k-th digit represents a multiple of 10
k. (Digits are numbered from right to left, where the least significant digit is number 0.) For example,

When a number is expressed in binary, the
k-th digit represents a multiple of 2
k. For example,

In
skew binary, the
k-th digit represents a multiple of 2
k+1 - 1. The only possible digits are 0 and 1, except 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.)InputThe 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.OutputFor each number, output the decimal equivalent. The decimal value of n will be at most 2
31 - 1 = 2147483647.Sample Input
1012020000000000000000000000000000010100000000000000000000000000000011100111110000011100001011011020000
Sample Output
44214748364632147483647471041110737
直接模拟,注意些小技巧。完整代码:
/*UVa: 0.015s*//*POJ: 16ms,164KB*//*ZOJ: 0ms,180KB*/#include<cstdio>#include<cstring>char skew[32];int main(void){while (scanf("%s", skew), strcmp(skew, "0")){int len = strlen(skew), ans = 0;for (int i = 0; i < len; i++)ans += (skew[i] & 15) * ((1 << len - i) - 1);printf("%d
", ans);}return 0;}