SKEW

Total time limit:
1000ms
Memory Limit:
65536kB
description
In skew binary representation, the value of k bits X k represents X k * (2 k +. 1 -1). The numbers above each bit may be 0 or 1, the final surface 2 may be a non-zero bit, e.g., 10120 (skew) = 1 * (2 . 5 -1) + 0 * (2 . 4 -1) + 1 * ( 2 . 3 -1) + 2 * (2 2 -1) + 0 * (2 . 1 -1) = 0 + 31 + 0 + 7 + 6 = 44. The number of the first ten skew is 0,1,2,10, 11,12,20,100,101, and 102.
Entry
Input comprising one or more rows, each row contains an integer n. If n = 0 represents the input end, otherwise n is a number skew
Export
For each input, output its decimal representation. Converted to decimal, n less than 2 31 is -1 = 2147483647
#include<iostream>
#include<string.h>
using namespace std;
 int main()
{
    int base[31];
    int m, n;
    char skew[32];
    base[0] = 1;
    for (m = 1; m < 31; m++)
    {
        base[m] = 2 * base[m - 1] + 1;
    }
    while (true)
    {
        cin >> skew;
        n = 0;
        if (strcmp(skew, "0") == 0) break;
        int len = strlen(skew);
        for (m = 0; m < len; m++)
        {
            n += base[len - m - 1] * (skew[m] - '0');
        }
        cout << n;
    }
    return 0;
}

str which is stored character numbers, which turn into numeric types
example str [I] is '1', in fact ascii code is 0x31, and the '0' ascii code is 0x30, using str [i] - '0' get 1

Comparative binary string conversion +

Guess you like

Origin www.cnblogs.com/hzy-bwd/p/11587964.html