标签:blog ar java for div 2014 c log ad
题目:本质是求两个数的最大公约数,java大数真好用 ^_^。
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger TWO = BigInteger.valueOf(2); Scanner t = new Scanner(System.in); int cas = t.nextInt(); for(int ca=1;ca<=cas;ca++) { String a=t.next(); String b=t.next(); BigInteger A = cal(a); BigInteger B = cal(b); BigInteger C = A.gcd(B); BigInteger []D = new BigInteger[1010]; int i=0; while(!C.equals(BigInteger.ZERO)) { D[i++] = C.mod(TWO); C = C.divide(TWO); } System.out.print("Case #"+ca+": " ); for(i--;i>=0;i--) System.out.print(D[i]); System.out.println(); } } public static BigInteger cal(String s) { char b = '1'; BigInteger t = BigInteger.valueOf(0); BigInteger k = BigInteger.valueOf(1); BigInteger TWO = BigInteger.valueOf(2); for(int i=s.length()-1;i>=0;i--) { if(s.charAt(i)==b) { t=t.add(k); } k=k.multiply(TWO); } //System.out.println(t); return t; } }
标签:blog ar java for div 2014 c log ad
原文地址:http://blog.csdn.net/knight_kaka/article/details/39642909