zoukankan      html  css  js  c++  java
  • hdu 5050 Divided Land

    题目:本质是求两个数的最大公约数,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;
        }
    }
    


  • 相关阅读:
    LOL 计蒜客
    cf1486 D. Max Median
    P3567 [POI2014]KUR-Couriers
    dp 求物品组合情况
    黑暗爆炸
    hdu5306 Gorgeous Sequence
    P4609 [FJOI2016]建筑师
    cf 1342 E. Placing Rooks
    重修dp-背包
    acwing 2154. 梦幻布丁
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7103530.html
Copyright © 2011-2022 走看看