标签:style http color io os ar java for sp
HDU5050Divided Land(java大数)
题目大意:给你一个长和宽的矩形,要求你把这片面积划分成多个边长为N的正方形,不能有剩余的空间。要求N尽量大。给你二进制的长和宽,输出的N也是二进制形式。
解题思路:求L,M的GCD。但是因为这题的输入是二进制数非常大,只是中间求GCD的时候转成十进制求得N,并且输出N也是二进制形式。
代码:
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
public static BigInteger GCD (BigInteger num1, BigInteger num2) {
if (num2.equals(BigInteger.ZERO))
return num1;
return GCD(num2, num1.mod(num2));
}
public static void main(String args[]) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
int T;
T = cin.nextInt();
BigInteger L, W;
for (int i = 1; i <= T; i++) {
L = cin.nextBigInteger(2);
W = cin.nextBigInteger(2);
BigInteger ans = GCD(L, W);
cout.printf("Case #%d: ", i);
cout.println(ans.toString(2));
}
cin.close();
cout.close();
}
}
标签:style http color io os ar java for sp
原文地址:http://blog.csdn.net/u012997373/article/details/39644681