UVa 575/zoj 1712/mid-central USA 1997 Skew Binary (water ver.& skew binary)
575-skew Binary
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php? option=com_onlinejudge&itemid=8&category=24&page=show_problem&problem=516
http://poj.org/problem?id=1362
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1712
When a number is expressed in decimal, the k-th Digit represents a multiple ofK. (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 2K. For example,
In skew binary, the k-th Digit represents a multiple of 2K+1 -1. The only possible digits are 0 and 1, except which least-significant nonzero can be a 2. For example,
The numbers in skew binary are 0, 1, 2, A, A, m, M, and 102. (Skew binary is useful in some applications because it are possible to add 1 with at most one carry. However, this has the "do" with the current problem.)
Input
The input file contains one or more lines, and each of the which contains is an integer n. If n = 0 It signals the end of the input, and otherwise n are a nonnegative integer in skew binary.
Output
For each number, output the decimal equivalent. The decimal value of N'll is at most 2 -1 = 2147483647.
Sample Input
10120
200000000000000000000000000000
1000000000000000000000000000000
11111000001110000101101102000
0
Sample Output
3
2147483647
4
7
1041110737 2147483646
Direct simulation, attention to small skills.
Complete code:
/*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] &) * ((1 << len-i)-1);
printf ("%d\n", ans);
}
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/