HDU高精度總結(java大數類)

來源:互聯網
上載者:User

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);        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.