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ù)雏赦,求出它們的階乘并輸出。
實例 輸入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)行運算。
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());
}
}}