-
Notifications
You must be signed in to change notification settings - Fork 6
/
46_Accumulate.cpp
91 lines (69 loc) · 1.14 KB
/
46_Accumulate.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cstdio>
using namespace std;
class Temp
{
public:
Temp() { ++N; Sum += N;}
static void Reset() {N = 0; Sum = 0;}
static unsigned int GetSum() {return Sum;}
private:
static unsigned int N;
static unsigned int Sum;
};
unsigned int Temp::N = 0;
unsigned int Temp::Sum = 0;
unsigned int SumSolution1(unsigned int n)
{
Temp::Reset();
Temp *a = new Temp[n];
delete []a;
return Temp::GetSum();
}
class A;
A *Array[2];
class A
{
public:
virtual unsigned int Sum(unsigned int n)
{
return 0;
}
};
class B: public A
{
public:
virtual unsigned int Sum(unsigned int n)
{
return Array[!!n]->Sum(n - 1) + n;
}
};
int SumSolution2(unsigned int n)
{
A a;
B b;
Array[0] = &a;
Array[1] = &b;
unsigned int value = Array[1]->Sum(n);
return value;
}
typedef unsigned int (*fun)(unsigned int);
unsigned int Solution3Teminator(unsigned int n)
{
return 0;
}
unsigned int SumSolution3(unsigned int n)
{
static fun f[2] = {Solution3Teminator, SumSolution3};
return n + f[!!n](n - 1);
}
int main(void)
{
unsigned int n;
while (cin >> n)
{
unsigned int sum = SumSolution3(n);
cout << sum << endl;
}
return 0;
}