阶乘

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.

    Alice found that the factorial quickly becomes very large, and she want to find the rightmost non-zero digit of N!.


Input


 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. 


Output


 Each test case a single line containing a single digit: the right most non-zero digit of N!.


Sample Input


Copy sample input to clipboard


4
4
5
6
7


Sample Output


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;
	}	
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值