Many years later, Alice grow up and she become a high-school student.One day she learned factorial on math class. The factorial of an integer N, written N!, is the product of all the integers from 1 through N inclusive.For example, 4! = 1 * 2 * 3 * 4 = 24, Likewise, 5! = 1 * 2 * 3 * 4 * 5 = 120.
There are T(1 <= T <= 50) test cases. For each test case, A single positive integer N no larger than 10000 and no smaller than 1.
Each test case a single line containing a single digit: the right most non-zero digit of N!.
4 4 5 6 7
4 2 2 4
题意:找出某个数的阶乘最后边不为0的数
下面的做法是把所有因子中的2和5的个数储存起来,然后最后去判断2和5哪个多,能组成10的2和5的个数去掉,2多的话就可能乘以2,4,8,6这样的数,如果是5多的话就只可能乘5,这样就避免的后面有0的情况,是从网上找到的大虾的做法
#include <iostream>
#include <cmath>
#include <stack>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int main()
{
const int arr[5]={6,2,4,8};
int test,two,five,res;
cin >> test;
while(test--){
int n;
res=1;
five=0;
two=0;
cin >> n;
for(int i=2; i <= n; ++i){
int k=i;
while(k%2==0){
two++;
k/=2;
}
while(k%5==0){
five++;
k/=5;
}
res=(res*k)%10;
}
if(two>five){
res=(res*arr[(two-five)%4])%10;;
}
else if(two<five){
res=(res*5)%10;
}
cout << res << endl;
}
}