ACM中Java使用總結

來源:互聯網
上載者:User

Java在ACM中的主要應用是大數類【個人見解】。做個小總結,留作模板用。

類名預設為Main。

輸入:

聲明一個輸入對象cin;Scanner cin=new Scanner(System.in);

輸入一個int值:Int a=cin.nextInt();

輸入一個大數:BigDecimal a=cin.nextBigDecimal();

EOF結束:while(cin.hasNext()){。。。}

輸出:

輸出str【任意類型】:System.out.println(str);【有換行】System.out.print(str);【無換行】

System.out.println(”str“);【輸出字串str】

賦值:BigInteger a=new BigInteger(String.valueOf(12));

大數類特殊函數:

判斷相等:c.compareTo(BigDecimal.ZERO)==0【c等於】

判斷大於:c.compareTo(BigDecimal.ZERO)>0  【c大於0】

判斷小於:c.compareTo(BigDecimal.ZERO)<0  【c小於0】

修改格式:c.stripTrailingZeros().toPlainString();【c去除末尾0,並轉轉乘字串】

字串函數:

str.startWith("0").【以0開始】str.endWith("0")【以0結束】

str.subString(int x,int y)【從x到y的str的子串】str.subString(int x))【從x到結尾的str的子串】

BigInteger的函數:

構造:public BigInteger(String str)【把string類型資料轉變為大整數】

構造:public BigInteger pow(BigDecimal b)【b次方】

加:public BigInteger add(BigInteger b)【加上b】

減:public BigInteger subtract(BigInteger b)【減去b】

乘:public BigInteger  multiply(BigInteger b)【乘b】

除:public BigInteger divided(BigInteger b)【除b】

最大值:public BigInteger max(BigInteger a)【返回最大值】

最小值:public BigInteger min(BigInteger a)【返回最小值】

除:public BigInteger [] dividedandRemainder(BigInteger b)【除b,數組第一位是商,第二位是餘數】

BigDecimal的函數:

構造:public BigDecimal(String str)【把string類型資料轉變為高精度數】

            public BigDecimal(int str)【把int 類型資料轉變為大整數】

           public BigDecimal(double str)【把double類型資料轉變為大整數】

次方:public BigDecimal pow (BigDecimal b)【b次方】

加:public BigDecimal add(BigDecimal b)【加上b】

減:public BigDecimal subtract(BigDecimal b)【減去b】

乘:public BigDecimal multiply(BigDecimal b)【乘b】

除:public BigInteger divided(BigInteger b)【除b】

舉例:

A+B。輸出最簡結果

import java.math.BigDecimal;//引入高精度數的包import java.util.*;//輸入輸出所在的包【幾乎所有的代碼都必須加的包】public class Main {public static void main(String []args){Scanner cin=new Scanner(System.in);BigDecimal a,b,c;while(cin.hasNext()){a=cin.nextBigDecimal();b=cin.nextBigDecimal();c=a.add(b);if(c.compareTo(BigDecimal.ZERO)==0) {System.out.println("0"); continue;}String str=c.stripTrailingZeros().toPlainString();if(str.endsWith(".")) str=str.substring(0,str.length()-1);System.out.println(str);}}} 

 A的階乘:

 import java.util.*;import java.math.*;public class Main{    public static void main(String args[])    {        int m,i;        Scanner in = new Scanner(System.in);        m=in.nextInt();        BigInteger sum = new BigInteger(String.valueOf(m));        for(i=m-1;i>0;i--)        {            BigInteger x =new BigInteger(String.valueOf(i));            sum=sum.multiply(x);        }        System.out.println(sum);    }}        

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.