Hdu 5050 Divided Land

时间:2024-01-13 22:16:56

题目要求就是做求两个二进制数的gcd,如果是用java的话,这题很简单。但也可以用C++做,只能先给自己留下这个坑了,还在研究c++的做法。

import java.math.BigInteger;
import java.util.Scanner; /**
* Created by emerald on 8/14/15.
*
*/
public class Main { public static void main(String []args) {
Scanner in = new Scanner(System.in);
BigInteger d1, d2;
String line, str[], a, b;
int T = in.nextInt(), Case = 0;
in.nextLine();
while(T!=0) {
T --;
line = in.nextLine();
str = line.split(" ");
System.out.print("Case #" + (++ Case) +": ");
if(str[0].equals(str[1])) {
System.out.println(str[0]);
continue;
}
a = str[0];
b = str[1];
d1 = new BigInteger(a, 2);
d2 = new BigInteger(b, 2);
System.out.println((d1.gcd(d2)).toString(2));
}
}
}