蓝桥杯 2014年 第五届 矩阵翻硬币(JAVA)
package provincial_2014B;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class Ten {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String n = scan.next();
String m = scan.next();
// 1~n中的平方数=floor(sqrt(n))
System.out.println(sqrt(n).multiply(sqrt(m)));
}
public static BigInteger sqrt(String num) {
// 算出num平方根的位数
int length = num.length();
int len = length/2;
if((length&1)==1)
len+=1;
char[] ans = new char[len];
Arrays.fill(ans, '0');
BigInteger n = new BigInteger(num);
for(int i = 0; i < len; i++) {
for(char j = '1'; j <= '9'; j++) {
ans[i] = j;
BigInteger a = new BigInteger(String.valueOf(ans));
BigInteger temp = a.pow(2);
if(temp.compareTo(n)>0) {
ans[i] --;
break;
} else if(temp.compareTo(n)==0) {
return a;
}
}
}
return new BigInteger(String.valueOf(ans));
}
}