国防科学技术大学第忘记叫啥杯了

1594: Factorials

Time Limit: 1 Sec Memory Limit: 128 MB

Submit: 207 Solved: 72

[Submit][Status][Web Board]

Description

The factorial of an integer N, written N!, is the product of all the integers from 1 through N inclusive. The factorial quickly becomes very large: 13! is too large to store in a 32-bit integer on most computers, and 70! is too large for most floating-point variables. Your task is to find the rightmost non-zero digit of n!. For example, 5! = 1 * 2 * 3 * 4 * 5 = 120, so the rightmost non-zero digit of 5! is 2. Likewise, 7! = 1 * 2 * 3 * 4 * 5 * 6 * 7 = 5040, so the rightmost non-zero digit of 7! is 4.

Input

This problem includes multiple cases. The first line of input is a integer T represents the number of cases.

For each case, there is a single positive integer N no larger than 4,220.

Output

For each case, output corresponding results separately.

The output of each case is a single line containing but a single digit: the right most non-zero digit of N!.

Sample Input

1

7

Sample Output

4​

#include<stdio.h>

#include<string.h>

#include<iostream>

#include<math.h>

#include<algorithm>

using namespace std;

int a[100000];

int main()

{

int t,n;

scanf("%d",&t);

while(t--)

{

scanf("%d",&n);

int i,j,k,m;

long long g,t;

double s;

s=0;

for(k=2;k<=n;k++)

s+=log10(k);

m=(int)s+1;//对数累加,至于为啥我也不清楚,感觉挺实用的,求阶乘的位数

for(k=1;k<=m;k++)

{

a[k]=0;

}

a[1]=1;

g=0;

for(k=2;k<=n;k++)

{

for(j=1;j<=m;j++)

{

t=a[j]*k+g;//数组累乘并进位

a[j]=t;

g=t/10;

}

}

for(int i=1;i<=m;i++)

{

if(a[i]!=0)

{

printf("%d\n",a[i]);

break;

}

}

}

return 0;

}

                                                               

你可能感兴趣的:(国防科学技术大学第忘记叫啥杯了)