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 OutputStreamWriter(System.out)));

    public static void main(String[] args) throws IOException {
        Scan scan = new Scan();
        int t = scan.nextInt();
        for (int c = 1; c <= t; c++) {
            String s = scan.next();
            BigInteger a = new BigInteger(s,2);
            s = scan.next();
            BigInteger b = new BigInteger(s,2);
            out.println("Case #" + c + ": " + a.gcd(b).toString(2));
        }
        out.flush();
    }

    private static double max(double d, double e) {
        if (d - e > 0)
            return d;
        return e;
    }
}

class Scan {

    BufferedReader buffer;
    StringTokenizer tok;

    Scan() {
        buffer = new BufferedReader(new InputStreamReader(System.in));
    }

    boolean hasNext() {
        while (tok == null || !tok.hasMoreElements()) {
            try {
                tok = new StringTokenizer(buffer.readLine());
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }

    String next() {
        if (hasNext())
            return tok.nextToken();
        return null;
    }

    String nextLine() {
        String s = null;
        try {
            s = buffer.readLine();
        } catch (Exception e) {
            return null;
        }
        return s;
    }

    char nextChar() {
        try {
            return ((char) buffer.read());
        } catch (Exception e) {
        }
        return ‘\0‘;
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }
}

从大牛哪里学来了模板,,速度之快就不用多说了

学习了,JAVA模板还是很重要的,高精度模板也是

  

时间: 2024-10-14 18:52:23

HDU 5050 Divided Land ( JAVA )的相关文章

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(进制转换)

题意  给你两个二进制数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.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

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 4927 Series 1 java大数

java mle前会wa 或者 t 这种事我会乱说? import java.math.*; import java.util.*; import java.io.*; public class Main { BigInteger[] a = new BigInteger[3007]; public void work() { int T; T = cin.nextInt(); while (T-- > 0) { int n; n = cin.nextInt(); for (int i = 0;

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