1199(阶乘结果的最后一位非零数)

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

int lastNonZeroDigitOfFactorial(const int& n)
{
	static int r[]={6,2,4,8};
	int m=n,a;
	for(a=0;m;m/=5)
		a+=(m%5==2)+(m%5==4)*2+m/5;
	return n>1?r[a%4]:1;
}
int main()
{
	vector<int> ivec;
	int b;
	while(cin>>b)
	{
		ivec.push_back(b);
		ivec.push_back(lastNonZeroDigitOfFactorial(b));
	}
	vector<int>::const_iterator iter = ivec.begin();
	while(iter != ivec.end())
	{
		cout<<setw(5)<<setiosflags(ios::right)<<*iter++;
		cout<<" -> "<<*iter++<<endl;
	}		
	return 0;
}

---------------------

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
const int N = 10000;
int a[N+1];

void init()
{
	int i = 0;
	a[0] = 1;
	int tmp = -1;
	for(i=1;i<=N;i++)
	{
		tmp = a[i-1]*i;		
		while(tmp%10==0)
			tmp/=10;
		a[i] = tmp%100000;
	}
}
int main()
{
	vector<int> ivec;
	init();
	int b;
	while(cin>>b)
	{
		ivec.push_back(b);
		ivec.push_back(a[b]%10);
	}
	vector<int>::const_iterator iter = ivec.begin();
	while(iter != ivec.end())
	{
		cout<<setw(5)<<setiosflags(ios::right)<<*iter++;
		cout<<" -> "<<*iter++<<endl;
	}		
	return 0;
}
--------------------------
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

int lastNonZeroDigitOfFactorial(const int& n) {
	int exponentOfTwo = 0,
		exponentOfFive = 0,
		lastNonZeroDigit = 1;

	for ( int i = 1; i <= n; i++ ) {
		int temp = i;

		while ( temp % 2 == 0 ) {
			temp /= 2;
			exponentOfTwo++;
		}
		while ( temp % 5 == 0 ) {
			temp /= 5;
			exponentOfFive++;
		}
		lastNonZeroDigit = lastNonZeroDigit * ( temp % 10 ) % 10;
	}

	for ( int count = 0; count < exponentOfTwo - exponentOfFive; count++ )
		lastNonZeroDigit = lastNonZeroDigit * 2 % 10;

	return lastNonZeroDigit;
}

int main()
{
	vector<int> ivec;
	int b;
	while(cin>>b)
	{
		ivec.push_back(b);
		ivec.push_back(lastNonZeroDigitOfFactorial(b));
	}
	vector<int>::const_iterator iter = ivec.begin();
	while(iter != ivec.end())
	{
		cout<<setw(5)<<setiosflags(ios::right)<<*iter++;
		cout<<" -> "<<*iter++<<endl;
	}		
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值