"The sword refers to offer" asks 1+2+3+...+n

Source: Internet
Author: User

"Disclaimer: All rights reserved, please indicate the source of the reprint, do not use for commercial purposes. Contact mailbox: [Email protected] "


Topic Link:http://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1?rp=3&ru=/ta/coding-interviews& Qru=/ta/coding-interviews/question-ranking


Title Description
Ask for 1+2+3+...+n, the request cannot use multiplication method, for, while, if, else, switch, case and other keywords and conditional judgment statement (A? B:C).

Ideas
It seems like there is no good way to think about it at first, but if we are familiar with C + + features, or if we know more about recursion, we can find some ways to solve this problem.

1. Using recursion

Class Solution{public:int sum_solution (int n) {int Sum = N;bool flag = (n>0) && ((Sum+=sum_solution (n-1)) >0) ; return sum;}};

2. Constructors

Class temp{public    :        Temp ()        {            ++n;            sum+=n;        }        static void Reset ()        {            N = Sum = 0;        }        static int getsum ()        {            return Sum;        }    Private:        static int N;        static int Sum;}; int temp::n = 0;int temp::sum = 0; Class solution{public    :        int sum_solution (int n)        {            temp::reset ();            Temp *a = new Temp[n];            delete []a;            A = NULL;            return Temp::getsum ();        }};

3. Virtual functions

Class A; A * array[2]; Class a{public    :        virtual int Sum (int n)        {            return 0;        }}, Class B:public a{public    :        virtual int Sum (int n)        {            return array[!! N]->sum (n-1) +n;        }; Class solution{public    :        int sum_solution (int n)        {            a A;            b b;            Array[0] = &a;            ARRAY[1] = &b;            return Array[1]->sum (n);        }};


Copyright NOTICE: This article is the original blogger article, if reproduced, please indicate the source

"The sword refers to offer" asks 1+2+3+...+n

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.