杭電ACM 1018 BigNumber

Problem Description

In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.

Input

Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 on each line.

Output

The output contains the number of digits in the factorial of the integers appearing in the input.

Sample Input

2

10

20

Sample Output

7

19

首先分析一下這個題目的具體內(nèi)容,輸入一個數(shù)N,接著輸入N個數(shù),求出它們的階乘并輸出。
實(shí)例 輸入2 ,然后輸入兩個數(shù)10、20,輸入為7、19,分別為10的階乘結(jié)果的位數(shù)和20的階乘結(jié)果的位數(shù)。
由于階乘的結(jié)果是一個大數(shù),Int類型無法解決,生活中大數(shù)的應(yīng)用有很多,Java中有一個類BigIntegera表示大整數(shù)類。理論上可以表示無限大的數(shù),用這個解決十分方便。

以下是關(guān)于BigIntegera的用法:

Ⅰ基本函數(shù):

1.valueOf(parament); 將參數(shù)轉(zhuǎn)換為制定的類型
   比如 int a=3;
   BigInteger b=BigInteger.valueOf(a);
   則b=3;
   String s=”12345”;
   BigInteger c=BigInteger.valueOf(s);
   則c=12345;
 2.add(); 大整數(shù)相加
   BigInteger a=new BigInteger(“23”);
   BigInteger b=new BigInteger(“34”);
   a. add(b);
 3.subtract(); 相減
 4.multiply(); 相乘
 5.divide();    相除取整 
 6.remainder(); 取余
 7.pow();   a.pow(b)=a^b 
 8.gcd();   最大公約數(shù)
 9.abs(); 絕對值
 10.negate(); 取反數(shù)
 11.mod(); a.mod(b)=a%b=a.remainder(b);
 12.max(); min();
 13.punlic int comareTo();
 14.boolean equals(); 是否相等
 15.BigInteger構(gòu)造函數(shù):
   一般用到以下兩種:
   BigInteger(String val);
   將指定字符串轉(zhuǎn)換為十進(jìn)制表示形式;
   BigInteger(String val,int radix);
   將指定基數(shù)的 BigInteger 的字符串表示形式轉(zhuǎn)換為 BigInteger

Ⅱ.基本常量:

A=BigInteger.ONE    1
B=BigInteger.TEN    10
C=BigInteger.ZERO   0

Ⅲ.基本操作

讀入:
用Scanner類定義對象進(jìn)行控制臺讀入,Scanner類在java.util.*包中

Scanner cin=new Scanner(System.in);// 讀入
while(cin.hasNext())   //等同于!=EOF
{
   int n;
   BigInteger m;
   n=cin.nextInt(); //讀入一個int;
   m=cin.BigInteger();//讀入一個BigInteger;
System.out.print(m.toString());
}

下面是我用java寫的解決方法,主要是通過for循環(huán)求出某個數(shù)的階乘,

注意大數(shù)中的 1 的定義為 BigInteger a=BigInteger.ONE;

Int 類型通過String.valueOf()轉(zhuǎn)化再定義為BigInteger類型的數(shù)才可以與BigInteger類型的數(shù)進(jìn)行運(yùn)算。

public class Bigdata {    
public static void main(String[] args){        
    Scanner in = new Scanner(System.in);       
    int num = in.nextInt();        
    for(int i=0;i<num;i++ ){            
        int text = in.nextInt();            
        BigInteger a=BigInteger.ONE;           
        for(int j=1;j<=text;j++){                
            BigInteger c = new BigInteger(String.valueOf(j));                
            a = a.multiply(c);            
    }            
    System.out.println(String.valueOf(a).length());        
}    
}}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 其實(shí)窮真的是原罪。我看不上別人拿窮說事,說窮不是你逃避的理由,不是你不行的理由,不是你做任何事的理由,那時真的很自...
    供養(yǎng)人閱讀 1,164評論 0 0
  • 當(dāng)全世界都叫囂著去旅行的時候,我也沒能免俗。Backpack這顆種子大概是在初中結(jié)業(yè)那個沒有作業(yè)的暑假埋下的。那時...
    Susie910閱讀 360評論 1 2
  • 最近一直在商量國慶的計劃。 本來這篇文章是在九月十日左右就迸發(fā)出來的想法,因?yàn)槟翘煳页晒Φ仄垓_了高中同學(xué)買了來長沙...
    梓楚陽閱讀 612評論 0 1
  • 最近在看一個線上產(chǎn)品學(xué)習(xí)視頻,課后作業(yè)就是分析簡書這款產(chǎn)品…所以有了我這篇文章……
    Alice_a閱讀 286評論 0 0

友情鏈接更多精彩內(nèi)容