Translate

Views

Sunday, June 11, 2023

Solution UVA: 11933 - Splitting Numbers

 

 Last Submissions
  Problem  VerdictLangTimeBestRankSubmit Time
 | discuss11933 - Splitting Numbers AcceptedC++110.0000.000272747 secs ago


Problem: 11933

Suggest: http://programalgorithms.blogspot.com/

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


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

int n;

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    while(cin >> n && n){
        string s = bitset<32>(n).to_string();
           
        int rank = 0;
        string a = "";
        string b = "";
   
        int pos = 0;
        while(s[pos]=='0') pos++;
   
        for(int i=s.size()-1; i>=pos; i--){
            if (s[i]=='1'){
               
                rank++;
                if (rank%2){
                    a='1'+a;
                    b='0'+b;
                }  
                else{
                    b='1'+b;    
                    a='0'+a;
                }

            }
            else{
                a='0'+a;
                b='0'+b;
            }
        }
        cout<<stoi(a, 0, 2)<<" "<<stoi(b, 0, 2)<<"\n";
    }
}

No comments: