"Sword refers to offer": 1 + 2 + 3 +... + n, "Sword refers to offer"

Source: Internet
Author: User

"Sword refers to offer": 1 + 2 + 3 +... + n, "Sword refers to offer"

[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]


Question link: http://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1? Rp = 3 & ru =/ta/coding-interviews & qru =/ta/coding-interviews/question-ranking


Description
Evaluate 1 + 2 + 3 +... + n. It is required that the keyword and condition judgment statement (? B: C ).

Ideas
At the beginning of this question, it seems that we can't think of any great method, but if we are familiar with the features of C ++ or have a better understanding of recursion, we can still find some solutions.

1. Use 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. Constructor

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 Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source

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.