Algorithm-skew number

Source: Internet
Author: User

Algorithm-skew keyword one key word two

    • topic
      • input
      • output
      • source
    • full code

Title Description

In the skew binary representation, the value of the K-bit XK represents xk* (2^ (k+1)-1). The number of possible digits on each bit is 0 or 1, and the last face of a non 0 bit can be 2, for example, 10120 (skew) = 1* (2^5-1) + 0* (2^4-1) + 1* (2^3-1) + (2^2-1) + 0* (2^1-1) = 31 + 0 + 7 + 6 + 0 = 44. The first 10 skew numbers are 0, 1, 2, 10, 11, 12, 20, 100, 101, and 102.

Input

The input contains one or more rows, and each row contains an integer n. If n = 0 means the input ends, otherwise n is a skew number

Output

For each input, output its decimal representation. After conversion to decimal, n does not exceed 2^31-1 = 2147483647

Sample input
1012020000000000000000000000000000010100000000000000000000000000000011100111110000011100001011011020000
Sample output
44214748364632147483647471041110737
Source

http://bailian.openjudge.cn/practice/2973

Key points of the problem solving method

The topic is actually a conversion problem, which can be encapsulated into a function, and my approach is to use a string to save the input and then start the calculation from the last one to get the output. The function declaration is as follows:

int calc(char *num);

The implementation is as follows:

 int calc(char *num) {intsum =0;int Base=2;intn = strlen (num); for(inti = n; i >0; i--) {intA = num[i-1] -' 0 '; A *= (Base-1); sum + = A;Base*=2; }returnsum;}

Specific conversion ideas can refer to my other article algorithm-to determine the binary.

Full code

The problem is relatively simple, the complete code is as follows:

////Main.cpp//2973:skew number////Created by Limao on 15/6/11.//Copyright (c) 2015 Limao. All rights reserved.//#include <string.h>#include <stdio.h>#include <iostream>using namespace STD; int calc(char *num) {intsum =0;intBase =2;intn =strlen(num); for(inti = n; i >0; i--) {intA = num[i-1] -' 0 '; A *= (base-1);                sum + = A; Base *=2; }returnsum;} int main(int argc, const Char * argv[]) {Charnum[ *]; while(scanf('%s ', num) && num[0] !=' 0 '){//For Output        printf("%d\n", calc (num)); }return 0;}

Algorithm-skew number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.