Title: for 1+2+...+n, multiplication and division, for, while, if, else, switch, case and other keywords and judgment statements are not required
Analysis:
Method (1): using a constructor. We define a class, in which we define two static member variables, let the constructor operate these two static member variables, and then create n objects to complete what we want.
Method (2): using virtual function. Simulate recursion, but the exit condition can't be judged by conditional statement. We use two functions to simulate (one simulation continues recursion, the other simulation exit condition) when n is not 0, call B:sum(), otherwise call A:sum(), which is equivalent to recursive exit.
Method (3): consistent with method (2), only at the level of C language, function pointer (array) can only be used to simulate to decide which function to call (also a function simulation recursively calls a function simulation exit condition)
Method (4): using non type template (with specialization) type. The general idea of template simulation recursion and specialized simulation recursion exit is recursion + recursion exit (and these two steps need not be judged by conditional statements). The time complexity of this method using non type template is O(1), because it is calculated at compile time, but the disadvantage is that n must be a constant that can be determined at compile time, not a variable, and the compiler's delivery of recursively compiled code The return depth is limited.
class Sum { private: static int n; static int sum; public: Sum() { n++; sum += n; } static int ret() { return sum; } }; int Sum::n = 0; int Sum::sum = 0; int s_sum(int n) { Sum* p = new Sum[n]; delete[]p; p = nullptr; //Because only non static member functions can be called like this //Non static members can only be called with objects, so the //ret() is defined as a static member function return Sum::ret(); }
class A { public: virtual int sum(int n)//Exit { return 0; } }; A* a[2];//Pointer array class B :public A { public: virtual int sum(int n)//recursion { return n + a[!!n]->sum(n - 1); } }; int s_sum(int n) { A a1; B b1; a[0] = &a1; a[1] = &b1; //a[1] is the pointer of the parent class (and the sub class completes the virtual function reconstruction), which forms a polymorphism. When n is not equal to 0, call //B::sum() is recursive. When n is equal to 0, call A::sum() which is the recursion exit //Here!! n plays a key role. When n==0, a [! N] = = a [0], that is, when the parent pointer points to the parent object, the parent class A::sum() will be called int ret = a[1]->sum(n); return ret; }
typedef int(*pfun)(int);//Define function pointer type, function type return value is int, parameter is an int int fun1(int n)//Analog exit { return 0; } int fun2(int n) { static pfun f[2] = {fun1, fun2};//Function pointer array return n + f[!!n](n - 1);//N non-0 call fun2(n-1) is recursive, 0 call fun1() is recursive exit }
template<size_t n> class Sum { public: //Anonymous enumeration (you can take the variables directly) //The last member of the enumeration does not, enum { ret = n + Sum<n - 1>::ret }; }; //When n = 1 (export) template<> class Sum<1> { public: enum { ret = 1 }; };