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模板还是很重要的,高精度模板也是

  

你可能感兴趣的:(java)