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;
    }
}
时间: 2024-10-21 03:06:02

hdu 5050 Divided Land的相关文章

HDU 5050 Divided Land ( JAVA )

HDU 5050 - Chinese Girls' Amusement ( JAVA or 高精度 ) 题意不用再解释 做法是求两个二进制数的最大公约数字 然后以二进制输出 import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStrea

HDU 5050 Divided Land(进制转换)

题意  给你两个二进制数m,n   求他们的最大公约数  用二进制表示  0<m,n<2^1000 先把二进制转换为十进制  求出最大公约数  再把结果转换为二进制  数比较大要用到大数 import java.util.*; import java.math.*; public class wl6_9 { static BigInteger two = BigInteger.valueOf(2), one = BigInteger.ONE, zero = BigInteger.ZERO; s

HDU 5050 Divided Land(最大公约数Java)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5050 Problem Description It's time to fight the local despots and redistribute the land. There is a rectangular piece of land granted from the government, whose length and width are both in binary form.

hdu 5050 Divided Land(JAVA高精度)

import java.util.Scanner; import java.math.BigInteger; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(System.in); int T; T=cin.nextInt(); BigInteger a,b; for(int cas=1;cas<=T;cas++){ a=cin.nextBigInteger(2); b=ci

hdu 5050 Divided Land---2014acm上海赛区网络赛

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5050 Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 115    Accepted Submission(s): 57 Problem Description It's time to fight the loc

HUD 5050 Divided Land

http://acm.hdu.edu.cn/showproblem.php?pid=5050 题目大意: 给定一个矩形的长和宽,把这个矩形分成若干相等的正方形,没有剩余.求正方形的边长最长是多少. 解题思路: 这道题是“pick定理”的一个变种(不知道是pick定理,也可以退出结论).由定理是求矩形的长和宽最大公约数.但是这道题,输入的数是二进制, 输出也是二进制,二进制数的大小为2^1000,所以是大数的算法.java有大数的处理. 1 import java.math.BigInteger;

hdu----(5050)Divided Land(二进制求最大公约数)

Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 123    Accepted Submission(s): 64 Problem Description It’s time to fight the local despots and redistribute the land. There is a rect

HDU 5050

http://acm.hdu.edu.cn/showproblem.php?pid=5050 大数gcd import java.io.* ; import java.math.* ; import java.util.* ; import java.text.* ; public class Main { public static void main(String[] args) { Scanner cin=new Scanner (new BufferedInputStream(Syste

HDU 5050 Java 大数类+读入二进制

http://acm.hdu.edu.cn/showproblem.php?pid=5050 才知道 原来JAVA这么好用,连二进制都封装好了 in.nextBigInteger(2)  就是按二进制读入 当然a.gcd(b).toString(2)  是按二进制输出一个二进制String,  a,b是大数 另外,JAVA交代码原来是class Main import java.math.BigInteger; import java.util.Scanner; public class Mai