題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=14&page=show_problem&problem=1161
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
| #include <iostream> #include <vector> #include <string>
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; }
int factorial_sum(int n){ if(n == 0) return 1;
vector<int> v = {1};
for(int i=2; i<=n; ++i){ multiply(v, i); }
int sum = 0;
for(int i=0; i<v.size(); ++i){ sum += v[i]; }
return sum; }
int main(){ int n, ans; while(cin >> n){ ans = factorial_sum(n); cout << ans << '\n'; } return 0; }
|