Question: 1 + 2 + 3... + N. It is required that keywords such as multiplication and division, for, while, if, else, switch, case, and condition judgment statements cannot be used.
Problem Analysis:
You cannot use multiplication or division, so you cannot use the n (n + 1)/2 formula.
If you cannot use for while, you cannot use loops or the like.
Use the constructor to solve the problem:
Loop only allows the same code to be repeatedly executed n times. We can define a type and create n instances of this type. At this time, the class constructor will certainly execute n times,
We accumulate N times in the class constructor.
Note: The member variables involved in the accumulation should be static, because each class is unique, rather than each instance.
Class temp {public: temp () {sum + = I; ++ I;} static int getsum () {return sum;} PRIVATE: static int sum; static int I ;}; int temp: Sum = 0; int temp: I = 1; int sum (INT N) {temp * A = new temp [N]; int result = temp: getsum (); Delete [] A; A = NULL; return result ;}