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