Question: 1 + 2 +... + N. the keyword such as multiplication and division, for, while, if, else, switch, and Case and the condition judgment statement (? B: C ).
Usually 1 + 2 +... + N in addition to formula n (n + 1)/2, there are two methods: loop and recursion. Since the use of for and while has been explicitly restricted, the loop can no longer be used. Likewise, recursive functions also need to use the if statement or condition-based judgment statement to determine whether to continue or terminate recursion. However, these two statements are no longer allowed in the question.
Constructors using classes
View code # Include < Iostream >
Using NamespaceSTD;
Class Sum
{
Public :
Sum ()
{
Sum + = ++ N;
}
Static Void Reset ();
Static Int Getsum ();
Private :
Static Int N;
Static Int SUM;
};
Void Sum: reset ()
{
N = 0 ;
Sum = 0 ;
}
IntSum: getsum ()
{
ReturnSUM;
}
int sum :: n = 0 ;
int sum :: sum = 0 ;
int sum ( const int & N)
{< br> sum * S = New sum [N];
Delete [] S;
return sum: getsum ();
}
IntMain ()
{
Cout<Sum (100)<Endl;
Return 0;
}