41.求1+2+3+...+n
题目内容:
代码及思路:
#include<iostream>
#include<vector>
using namespace std;
class Solution
{
typedef int(*fun)(int);
public:
//利用函数指针来求解
static int Solution3_terminator(int n)
{
return 0;
}
static int Sum_Solution(int n)
{
fun f[2] = { Solution3_terminator,Sum_Solution };
return n + f[!!n](n - 1);
}
};
void main()
{
Solution* object = new Solution;
int n;
cin >> n;
int res = object->Sum_Solution(n);
cout << res << endl;
}