Factorials
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.
PROGRAM NAME: fact4
INPUT FORMAT
A single positive integer N no larger than 4,220.
SAMPLE INPUT (file fact4.in)
7
OUTPUT FORMAT
A single line containing but a single digit: the right most non-zero digit of N! .
SAMPLE OUTPUT (file fact4.out)
4
这道题,嘿嘿,套用高精模板,每次计算后把后面的0擦去,然后再对10000取余即可……
因为长度问题,我就不贴我的高精模板了……
/*
USER: wangyuc2
TASK: fact4
LANG: C++
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.022 secs, 2868 KB]
Test 2: TEST OK [0.000 secs, 2872 KB]
Test 3: TEST OK [0.000 secs, 2868 KB]
Test 4: TEST OK [0.011 secs, 2872 KB]
Test 5: TEST OK [0.011 secs, 2872 KB]
Test 6: TEST OK [0.054 secs, 2872 KB]
Test 7: TEST OK [0.076 secs, 2868 KB]
Test 8: TEST OK [0.065 secs, 2868 KB]
Test 9: TEST OK [0.076 secs, 2872 KB]
Test 10: TEST OK [0.076 secs, 2872 KB]
All tests OK.
Your program ('fact4') produced all correct answers! This is your
submission #2 for this problem. Congratulations!
*/
#include <fstream>
#include <iostream>
#include <cstring>
#include <memory>
#include <algorithm>
#define cin fin
using namespace std;
string operator+(string x, string y);
string operator-(string x, string y);
string operator*(string x, string y);
string operator*(string s, int a);
string operator/(string s, int a);
int operator%(string s, int a);
string operator/(string x, string y);
string operator%(string x, string y);
ifstream fin("fact4.in");
ofstream fout("fact4.out");
int main()
{
string s,s1;
int i,j,k,n,m,maxnum=0;
cin>>n;
s.assign("1");
s1.assign("10000");
for(i=1;i<=n;i++)
{
s=s*i;
j=s.size()-1;
// cout<<s<<' ';
while(s[j]=='0') {s.erase(j,j+1);j--;}
// cout<<s<<endl;
s=s%s1;
}
fout<<s[s.size()-1]<<endl;
// system("PAUSE");
return 0;
}