SGU 126. Boxes



http://acm.sgu.ru/problem.php?contest=0&problem=126

There are two boxes. There are A balls in the first box, and B balls in the second box (0 < A + B < 2147483648). It is possible to move balls from one box to another. From one box into another one should move as many balls as the other box already contains. You have to determine, whether it is possible to move all balls into one box.

#include <iostream>

using namespace std;

int solve_boxs(int a, int b){	
    int num=0;
    while(a!=0 && b!=0){
        if(((a+b)&1)==1)return -1;//a,b的和是奇数则无解
        if(a<b){
           b=b-a;
           b>>=1;
        }else{
           a=a-b;
           a>>=1;
        }
	num++;
    }
    return num;
}


int main(){
	int a;
	int b;
	while(cin>>a>>b){
		cout << solve_boxs(a,b)<<endl;
	}
	return 0;
}

点赞