HDU高精度总结(java大数类)

HDU1002 

http://acm.hdu.edu.cn/showproblem.php?pid=1002

代码:

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

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


HDU1042

http://acm.hdu.edu.cn/showproblem.php?pid=1042

代码:

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

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        BigInteger a,i,t;
        BigInteger one = new BigInteger("1");
        while(cin.hasNextBigInteger()){
            t=cin.nextBigInteger();
            i=new BigInteger("1");
            a=new BigInteger("1");
            int cmp=i.compareTo(t);
            while(cmp<=0){
                a=a.multiply(i);
                i=i.add(one);
                cmp=i.compareTo(t);
            }
            System.out.println(a);
        }
    }
}

HDU1047

http://acm.hdu.edu.cn/showproblem.php?pid=1047

代码:

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

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        BigInteger a,b;
        int i,t;
        BigInteger zero = new BigInteger("0");
        while(cin.hasNextInt()){
            t=cin.nextInt();
            for(i=1;i<=t;i++){
                boolean flag=true;
                b=zero;
                while(flag){
                    a=cin.nextBigInteger();
                    if(a.compareTo(zero)==0)flag=false;
                    b=b.add(a);
                }                    
                System.out.println(b);
                if(i<t)System.out.println("");
            }
        }
    }
}

HDU1063

http://acm.hdu.edu.cn/showproblem.php?pid=1063

注意,最简形式是去掉后面的 0,以及小于 1 的小数的小数点前的 0

代码:

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n,i;
        BigDecimal r,a;
        BigDecimal one = new BigDecimal("1");
        while(cin.hasNextBigDecimal()){
            r=cin.nextBigDecimal();
            n=cin.nextInt();
            a=one;
            for(i=1;i<=n;i++)
                a=a.multiply(r);
            a=a.stripTrailingZeros();
            String str=a.toPlainString();
            if(str.startsWith("0."))
                str=str.substring(1);
            System.out.println(str);
        }
    }
}

HDU1316

http://acm.hdu.edu.cn/showproblem.php?pid=1316

代码:

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

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int i,j;
        BigInteger a,b;
        BigInteger f[] = new BigInteger[501];
        BigInteger zero = new BigInteger("0");
        f[1]=new BigInteger("1");
        f[2]=new BigInteger("2");
        for(i=3;i<=500;i++)
            f[i]=f[i-1].add(f[i-2]);
        while(cin.hasNextBigInteger()){
            a=cin.nextBigInteger();
            b=cin.nextBigInteger();
            if(a.compareTo(zero)==0 && b.compareTo(zero)==0)break;
            for(j=0,i=1;i<=500;i++){
                if(a.compareTo(f[i])<=0 && b.compareTo(f[i])>=0)j++;
                if(b.compareTo(f[i])<0)break;
            }
            System.out.println(j);
        }
    }
}

HDU1715

http://acm.hdu.edu.cn/showproblem.php?pid=1715

代码:

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

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int i,j,t;
        BigInteger f[] = new BigInteger[1001];
        f[1]=new BigInteger("1");
        f[2]=new BigInteger("1");
        for(i=3;i<=1000;i++)
            f[i]=f[i-1].add(f[i-2]);
        while(cin.hasNextInt()){
            t=cin.nextInt();
            for(i=1;i<=t;i++){
                j=cin.nextInt();
                System.out.println(f[j]);
            }
        }
    }
}

HDU1753

http://acm.hdu.edu.cn/showproblem.php?pid=1753

代码:

同样的,小数的最简形式,要注意小于 1 的数的整数部分需要去掉

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

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        BigDecimal a,b;
        BigInteger zero = new BigInteger("0");
        while(cin.hasNextBigDecimal()){
            a=cin.nextBigDecimal();
            b=cin.nextBigDecimal();
            a=a.add(b);
            a=a.stripTrailingZeros();
            String str=a.toPlainString();
            if(str.startsWith("0."))
                str=str.substring(1);
            System.out.println(str);
        }
    }
}



你可能感兴趣的:(java,c,String,Class,import)