Saturday, June 4, 2016

uva 575 - Skew Binary - Solution hints

https://uva.onlinejudge.org/external/5/575.pdf

This problem is really confusing  . But it's easy just find the pattern . Here's you have been asked to find the decimal equivalent of a skew binary system . Follow the following steps :

1 . 1st of all you have to gain knowledge about skew binary system it' s like binary system but a little bit change is needed .

2 . We know that in binary to decimal conversion the i-th positioned number is multiplied by 2^i then one by one summing all the i-th positioned digit by multiplying 2^i we get the desired decimal system but for skew binary it's different . You have to find i-th position number by modding the whole number by 10 then you need to multiply the i-th positioned number by (2^i+1-1)  . that's the difference .

3 . After multiplying all the i-th positioned number by  (2^i+1-1) just add all the result together .

4 . The process mentioned above is for only short integers but here the input maybe bigger than 10^18 so you have to take input as string and implement your code according to that .

for more information about Skew binary system have a look to :

Skew Binary system-Wikipedia

An accepted code is given below for better understand .

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long int i,r,sum,l,n;
    string num;

    while(cin>>num && num!="0")
    {
        sum=0;
        i=0;
        l=num.length();

        while(num[i])
        {
            n=num[i]-48;
            sum+=n*(pow(2,(l-i))-1);
            i++;

        }

        printf("%lld\n",sum);

    }

    return 0;
}

No comments:

Post a Comment