100 question _ 08: 1 + 2 +... + N

Source: Internet
Author: User
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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.