题解 | #N的阶乘#
N的阶乘
http://www.nowcoder.com/practice/f54d8e6de61e4efb8cce3eebfd0e0daa
大整数乘法,这无脑了
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
string cal(string num, int n) {
string val = to_string(n), ansstr = "";
int len = num.size() * val.size() + 1;
int* ans = new int[len + 1];
for (int i = 0; i <= len; ++i)
ans[i] = 0;
reverse(num.begin(), num.end());
reverse(val.begin(), val.end());
for (int i = 0; i < num.size(); ++i) {
for (int j = 0; j < val.size(); ++j) {
ans[i + j] += (num[i] - '0') * (val[j] - '0');
}
}
for (int i = 0; i < len; ++i) {
ansstr = to_string(ans[i] % 10) + ansstr;
ans[i + 1] += ans[i] / 10;
}
ansstr = to_string(ans[len]) + ansstr;
int pos = ansstr.find_first_not_of('0');
if (pos == string::npos)
return 0;
else
return ansstr.substr(pos);
}
int main()
{
int n;
string ans = "";
while (cin >> n) {
ans = "1";
for (int i = 2; i <= n; ++i)
ans = cal(ans, i);
cout << ans << endl;
}
}