Question 100: 1 + 2 + ...... Sum of N (abnormal level limit)

Source: Internet
Author: User
# Include <iostream> using namespace STD; Class temp {public: // construct temp () {++ N; sum + = n ;}// reset static void reset () {n = 0; sum = 0;} // return and static int getsum () {return sum;} PRIVATE: static int N; static int sum;}; int temp :: n = 0; int temp: Sum = 0; int sum (int n) {temp: reset (); temp * A = new temp [N]; delete [] A; A = NULL; return temp: getsum ();} void main () {cout <sum (10) <Endl ;}

Restriction: multiplication and division are not allowed. For, while, if, switch, case are not allowed. conditional expressions are not supported ....

This method uses the New Keyword to automatically call the constructor, so that the sum operation can be placed in the constructor, and the static member variable belongs to a class instead of an object, the constructor is accumulated every time it is called.

 

 

 

 

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.