題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=8&page=show_problem&problem=564
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <iostream> #include <vector> #include <string> #include <algorithm>
using namespace std;
void multiply(vector<int> &v, int n){ int carry = 0;
for(int i=0; i<v.size(); ++i){ int product = v[i] * n + carry; v[i] = product % 10; carry = product / 10; }
while(carry > 0){ v.push_back(carry % 10); carry /= 10; }
return; }
string factorial(int n){ if(n == 0) return "1";
vector<int> result = {1};
for(int i=2; i<=n; ++i){ multiply(result, i); }
string resultStr = ""; for(size_t i=0; i<result.size(); ++i){ resultStr += to_string(result[i]); }
reverse(resultStr.begin(), resultStr.end()); return resultStr; }
int main(){ string str; int n; while(cin >> n){ str.clear(); str = factorial(n);
cout << n << "!\n" << str << '\n'; } return 0; }
|