Problem Description
source:https://uva.onlinejudge.org/external/119/11933.html
We define the operation of splitting a binary number n into two numbers a(n), b(n) as follows. Let 0 ≤ i1 < i2 < . . . < ik be the indices of the bits (with the least significant bit having index 0) in n that are 1. Then the indices of the bits of a(n) that are 1 are i1, i3, i5, . . . and the indices of the bits of b(n) that are 1 are i2, i4, i6, . . .
For example, if n is 110110101 in binary then, again in binary, we have a = 010010001 and b = 100100100.
Input
Each test case consists of a single integer n between 1 and 231 − 1 written in standard decimal (base 10) format on a single line. Input is terminated by a line containing ‘0’ which should not be processed.
Output
The output for each test case consists of a single line, containing the integers a(n) and b(n) separated by a single space. Both a(n) and b(n) should be written in decimal format.
Sample Input
25 5000 1000000 2000000000 2147483647 0
Sample Output
17 8 4360 640 671808 328192 1378124800 621875200 1431655765 715827882
Solution:
source:https://uva.onlinejudge.org/external/119/11933.html
We define the operation of splitting a binary number n into two numbers a(n), b(n) as follows. Let 0 ≤ i1 < i2 < . . . < ik be the indices of the bits (with the least significant bit having index 0) in n that are 1. Then the indices of the bits of a(n) that are 1 are i1, i3, i5, . . . and the indices of the bits of b(n) that are 1 are i2, i4, i6, . . .
For example, if n is 110110101 in binary then, again in binary, we have a = 010010001 and b = 100100100.
Input
Each test case consists of a single integer n between 1 and 231 − 1 written in standard decimal (base 10) format on a single line. Input is terminated by a line containing ‘0’ which should not be processed.
Output
The output for each test case consists of a single line, containing the integers a(n) and b(n) separated by a single space. Both a(n) and b(n) should be written in decimal format.
Sample Input
25 5000 1000000 2000000000 2147483647 0
Sample Output
17 8 4360 640 671808 328192 1378124800 621875200 1431655765 715827882
Solution:
#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; }
No comments:
Post a Comment
Write your comment - Share Knowledge and Experience