java写大数总结

把一些涉及到大数的水题写了一番,以后遇到大数还是java大法方便啊。

HDU 1002

a+b大数版

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    void solve () {
        BigInteger a, b, c;
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt ();
        for (int i = 1; i <= t; i++) {
            System.out.println ("Case " + i + ":");
            a = cin.nextBigInteger ();
            b = cin.nextBigInteger ();
            System.out.println (a + " + " + b + " = " + a.add (b));
            if (i != t) System.out.println ();
        }
    }
    public static void main (String[] args) {
        Main work = new Main();
        work.solve ();
    }
}

HDU 1042

阶乘大数版

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    int maxn = 10005;
    void solve () {
        Scanner cin = new Scanner(System.in);
        int n;
        while (cin.hasNext()) {
            n = cin.nextInt ();
            BigInteger ans = BigInteger.valueOf (1);
            for (int i = 2; i <= n; i++) {
                ans = ans.multiply (BigInteger.valueOf (i));
            }
            System.out.println (ans);
        }
    }
    public static void main (String[] args) {
        Main work = new Main();
        work.solve ();
    }
}

HDU 1250

斐波那契数列大数版

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    void solve () {
        Scanner cin = new Scanner(System.in);
        BigInteger f1, f2, f3, f4, ans;
        while (cin.hasNext ()) {
            int n = cin.nextInt ();
            f1 = BigInteger.valueOf (1);
            f2 = f3 = f4 = ans = f1;
            if (n <= 4) {
                System.out.println ("1");
                continue;
            }
            for (int j = 5; j <= n; j++) {
                ans = f1.add (f2.add (f3.add (f4)));
                f1 = f2;
                f2 = f3;
                f3 = f4;
                f4 = ans;
            }
            System.out.println (ans);
        }
    }
    public static void main (String[] args) {
        Main work = new Main();
        work.solve ();
    }
}

HDU 1297

f(n)=f(n1)+f(n2)+f(n4)

import java.math.*;
import java.util.*;

public class Main {
    void solve () {
        Scanner cin = new Scanner(System.in);
        BigInteger[] ans = new BigInteger[1001];
        ans[1] = BigInteger.valueOf (1);
        ans[2] = BigInteger.valueOf (2);
        ans[3] = BigInteger.valueOf (4);
        ans[4] = BigInteger.valueOf (7);
        for (int i = 5; i <= 1000; i++) {
            ans[i] = ans[i-1].add (ans[i-2].add (ans[i-4]));
        }
        while (cin.hasNext ()) {
            int n = cin.nextInt ();
            System.out.println (ans[n]);
        }
    }
    public static void main (String[] args) {
        Main work = new Main();
        work.solve ();
    }
}

HDU 1715

还是斐波那契数列

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    void solve () {
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt ();
        BigInteger f1, f2, f3;
        for (int i = 0; i < t; i++) {
            int n = cin.nextInt ();
            f1 = BigInteger.valueOf (1);
            f2 = BigInteger.valueOf (1);
            f3 = BigInteger.valueOf (0);
            if (n == 1 || n == 2) {
                System.out.println ("1");
                continue;
            }
            for (int j = 3; j <= n; j++) {
                f3 = f1.add (f2);
                f1 = f2;
                f2 = f3;
            }
            System.out.println (f3);
        }
    }
    public static void main (String[] args) {
        Main work = new Main();
        work.solve ();
    }
}

HDU 1753

高精度小数,要去掉末尾的后导0.

import java.math.*;
import java.util.*;

public class Main {
    void solve () {
        //BigInteger a, b, c;
        Scanner cin = new Scanner(System.in);
        BigDecimal a = BigDecimal.valueOf (0);
        BigDecimal b = BigDecimal.valueOf (0);
        while (cin.hasNext ()) {
            a = cin.nextBigDecimal ();
            b = cin.nextBigDecimal ();
            System.out.println (a.add (b).stripTrailingZeros().toPlainString());
        }
    }
    public static void main (String[] args) {
        Main work = new Main();
        work.solve ();
    }
}

HDU 1865

f(n)=f(n1)+f(n2)

import java.math.*;
import java.util.*;

public class Main {
    void solve () {
        BigInteger a, b, c;
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt ();
        String s;
        for (int l = 1; l <= t; l++) {
            s = cin.next ();
            int n = s.length ();
            if (n == 1 || n == 2) {
                System.out.println (n);
                continue;
            }
            BigInteger f1 = BigInteger.valueOf (1);
            BigInteger f2 = BigInteger.valueOf (2);
            BigInteger f3 = BigInteger.valueOf (0);
            for (int i = 3; i <= n; i++) {
                f3 = f1.add (f2);
                f1 = f2;
                f2 = f3;
            }
            System.out.println (f3);
        }
    }
    public static void main (String[] args) {
        Main work = new Main();
        work.solve ();
    }
}

你可能感兴趣的:(java大数)