Friday, August 29, 2014

uva 11933 - Splitting Numbers: Algorithm and solve code C++

Algorithm:
uva 11933 - Splitting Numbers

This is a simple problem the operation of splitting a binary number n into two numbers a(n), b(n) .
you need to convert decimal to binary format for a given number and from that binary format create two numbers
a(n) and b(n). To build first value a(n) need to choice 1st  1  ,3rd  1, 5th  1,..... so on same position of original binary format other position fii by the value 0.
To build second value b(n) need to choice 2nd  1  ,4th  1, 6th  1,..... so on same position of original binary format other position fill by the value 0.
for example:
n=13
1     1     0     1    binary format
3     2     1     0    index

1(3rd 1)  0    0   1(1st 1)  For a(n)

0   1(2 nd 1)  0    0           For b(n)

So a= 9 and b=4

Problem :uva 11933
Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3084

Solve Code:

#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int main()
{
    int n,even,temp1,c,sum;
    while(scanf("%d",&n)==1)
    {

        if(n==0)
            break;
        temp1=n;
        c=0;
        even=0;
        sum=0;
        while(temp1!=0)
        {
            if(temp1%2==1)
            {
                even++;
                if(even%2==1)
                {
                    sum+=pow(2,c);
                }
            }
            temp1/=2;
            c++;
        }
        printf("%d %d\n",sum,n-sum);
    }
    return 0;
}




0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home