【CodeChef】Small factorials(BigInteger筆記)

來源:互聯網
上載者:User

標籤:style   blog   color   java   os   io   檔案   for   

You are asked to calculate factorials of some small positive integers.

Input

An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.

Output

For each integer n given at input, display a line with the value of n!

題解:題目一定是故意的,用了好多small,其實這道題用java裡面的BigInteger類才能夠過=。=

在這裡積累一下:

  1. 標頭檔:import java.math.BigInteger;
  2. BigInteger 表示任意大的整數,原則上是,只要你的電腦的記憶體足夠大,可以有無限位的
  3. 常用的值:BigInteger.ONE、BigInteger.TEN、BigInteger.ZERO
  4. 常用的函數:基本的加減乘除,模數,指數,左右移,與或非,異或等都有,用的時候查就試了。列舉常見的如下表
    Valueof 賦初值,從別的類型轉換過來
    add 加法
    subtract 減法
    multiply 乘法
    divide 除法
    remainder 餘數
    pow 指數
    gcd 公約數
    shiftLeft 左移
    xor 異或
    intValue 轉換成int
    equals(CompareTo) 判斷相等

這道題的代碼如下:

 1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.math.BigInteger; 5  6 public class Main { 7     public static void main(String[] args)throws IOException{ 8         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); 9         BigInteger[] fac = new BigInteger[101];10         fac[0] = fac[1] = BigInteger.ONE;11         12         for(int i = 2;i <= 100;i++)13         {14             BigInteger temp = BigInteger.valueOf(i);15             fac[i] = fac[i-1].multiply(temp);16         }17         18         int t = Integer.parseInt(bf.readLine());19         while(t-- > 0){20             int num = Integer.parseInt(bf.readLine());21             System.out.println(fac[num]);22         }23     }24 25 }

 

聯繫我們

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