-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_factorial_digit_sum.cpp
More file actions
70 lines (64 loc) · 1.1 KB
/
20_factorial_digit_sum.cpp
File metadata and controls
70 lines (64 loc) · 1.1 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> vi;
vi operator*(const vi& lhs, const vi& rhs);
vi& operator++(vi& v);
ostream& operator<<(ostream& os, const vi& rhs);
int main()
{
vi a(1,1);
vi i(1,1);
int j,n;
double sum=0;
for(j=0;j<100;++j,++i)
a = a*i;
n = a.size();
for(j=0;j<n;j++)
sum += a[j];
cout<<sum<<endl;
cout<<a<<endl;
}
vi& operator++(vi& v)
{
int carry = 1,n=v.size();
for(int i=0;carry && i<n;i++)
{
v[i]+=carry;
if(v[i] == 10)
{
v[i] = 0;
carry = 1;
}
else carry = 0;
}
if(carry)
v.push_back(1);
return v;
}
ostream& operator<<(ostream& os, const vi& rhs)
{
int i,n=rhs.size();
for(i=n-1;i>=0;i--)
os<<rhs[i];
return os;
}
vi operator*(const vi& lhs, const vi& rhs)
{
int i,j,m=lhs.size(),n=rhs.size();
int carry=0,digit;
vi res(m+n,0);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
res[i+j] += lhs[i]*rhs[j];
carry = 0;
for(i=0;i<m+n;i++)
{
res[i] += carry;
carry = res[i]/10;
res[i] = res[i]%10;
}
if(res[m+n-1]==0)
res.pop_back();
return res;
}