### Question

Sum up 1 to n without using division, multiplication, for loop, while loop, if else, switch and condition statement (A?B:C).

### Solution 1

Actually, for loop can be simulated through making use of the static property. Since static value will be stored in the class, we can increase the value every time we init a static class.

```#include <iostream.h>

class Temp
{
public:
Temp() {
++N;
Sum += N;
}
static void Reset() { N = 0; Sum = 0; }
static int GetSum() { return Sum; }

private:
static int N;
static int Sum;
};

int Temp::N = 0;
int Temp::Sum = 0;

int solution1_Sum(int n)
{
Temp::Reset();
Temp *a = new Temp[n];   //就是这个意思，new出n个数组。
delete []a;
a = 0;
return Temp::GetSum();
}

int main()
{
cout<<solution1_Sum(100)<<endl;
return 0;
}

//运行结果：
//5050
//Press any key to continue```

### Solution 2

```#include <iostream.h>

class A;
A* Array[2];

class A
{
public:
virtual int Sum (int n) { return 0; }
};

class B: public A
{
public:
virtual int Sum (int n) { return Array[!!n]->Sum(n-1)+n; }
};

int solution2_Sum(int n)
{
A a;
B b;
Array[0] = &a;
Array[1] = &b;
int value = Array[1]->Sum(n);
//利用虚函数的特性，当Array[1]为0时，即Array[0] = &a; 执行A::Sum，
//当Array[1]不为0时，即Array[1] = &b; 执行B::Sum。
return value;
}

int main()
{
cout<<solution2_Sum(100)<<endl;
return 0;
}

//5050
//Press any key to continue```