poj1423解題報告

Big Number
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26063   Accepted: 8323

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 <= m <= 10^7 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
題目的大體意思就是,讓你求一個數的階乘有多少位數,輸入格式第一個數代表你要輸入幾個數,然後接下來的數是你要求解的數。源代碼:
#include<iostream>
#include<cmath>
using namespace std;
void fun();
int num[10000001];
int main()
{
	int n,i,s;
	fun();
	cin>>n;
	while (n>=1)
	{
	    cin>>s;
		cout<<num[s]<<endl;
		n--;
	}
	return 0;
}
void fun()
{
    int i;
	double t=0;
	for (i=1;i<10000001;i++)
		    t+=log10((double)i);
		num[i]=(int)t+1;
	}
}
我這種方法屬於暴力方法,題目給出所有的數不超過10的7次方,我就把10的7次方以內所有的數用log10的方法全部算出來他們的位數,存到一個10的7次方的數組裏,你輸入什麼數就對應的數組的什麼位置,可以快速的得到你要的位數,我看網上有一種用公式的算法,我這種公式渣看到什麼pi的就果斷放棄了,雖然我的方法耗時間長,空間大,但是簡單粗暴,適合我們這些acm的新手~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章