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
首先分析一下這個題目的具體內容,輸入一個數(shù)N,接著輸入N個數(shù),求出它們的階乘并輸出。
實例 輸入2 ,然后輸入兩個數(shù)10、20,輸入為7、19,分別為10的階乘結果的位數(shù)和20的階乘結果的位數(shù)。
由于階乘的結果是一個大數(shù),Int類型無法解決,生活中大數(shù)的應用有很多,Java中有一個類BigIntegera表示大整數(shù)類。理論上可以表示無限大的數(shù),用這個解決十分方便。
以下是關于BigIntegera的用法:
Ⅰ基本函數(shù):
1.valueOf(parament); 將參數(shù)轉換為制定的類型
比如 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構造函數(shù):
一般用到以下兩種:
BigInteger(String val);
將指定字符串轉換為十進制表示形式;
BigInteger(String val,int radix);
將指定基數(shù)的 BigInteger 的字符串表示形式轉換為 BigInteger
Ⅱ.基本常量:
A=BigInteger.ONE 1
B=BigInteger.TEN 10
C=BigInteger.ZERO 0
Ⅲ.基本操作
讀入:
用Scanner類定義對象進行控制臺讀入,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()轉化再定義為BigInteger類型的數(shù)才可以與BigInteger類型的數(shù)進行運算。
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());
}
}}