-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum.cpp
More file actions
99 lines (81 loc) · 1.67 KB
/
Sum.cpp
File metadata and controls
99 lines (81 loc) · 1.67 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// 64 求 1+2+...+n
// 不允许使用可以进行条件判断的关键字及条件判断语句
// 构造函数
class Sum_v1 {
public:
Sum_v1()
{
++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 Sum_v1::N = 0;
unsigned int Sum_v1::Sum = 0;
unsigned int Sum_wrapper_v1(unsigned int n)
{
Sum_v1::Reset();
Sum_v1* a = new Sum_v1[n];
delete[] a;
a = nullptr;
return Sum_v1::GetSum();
}
// 虚函数
class A;
A* Array[2];
class A {
public:
virtual unsigned int Sum(unsigned int n)
{
return n;
}
};
class B : public A{
public:
virtual unsigned int Sum(unsigned int n)
{
return Array[!!n]->Sum(n - 1) + n;
}
};
// 非0时调用B::Sum 0时调用A::Sum.
int Sum_wrapper_v2(int n)
{
A a;
B b;
Array[0] = &a;
Array[1] = &b;
int res = Array[1]->Sum(n);
}
/// 函数指针
// 定义 fun为接受 unsigned int 返回 unsigned int 的函数指针
typedef unsigned int (*fun)(unsigned int);
unsigned int Sum_Terminator(unsigned int n)
{
return 0;
}
unsigned int Sum_wrapper_v3(unsigned int n)
{
// 定义函数指针数组
// 其余的与v2相似
static fun f[2] = { Sum_Terminator, Sum_wrapper_v3 };
return n + f[!!n](n - 1);
}
// 模板类型
// 编译时展开模板时完成计算 需要编译时常量
// n不能过大 因为模板的递归深度有限制
template <unsigned int n>
struct Sum_v4 {
enum Value { N = Sum_v4<n - 1>::N + n };
};
template <>
struct Sum_v4<1> {
enum Value { N = 1 };
};