Seeking 1+2+ by unconventional methods +n

Source: Internet
Author: User
Tags define constructor

Today in the "sword refers to offer" to see the following such a simple and interesting questions, to investigate the programmer's divergent thinking ability, if you are familiar with C + + knowledge, otherwise it is not to come out of the program, to share to everyone.

Title: Ask 1+2+ +n, the request cannot use multiplication and division method, for, while, if, else, switch, case and so on keyword and conditional judgment statement (A? B:C).

Comment: This problem does not have much practical significance in itself, because it is impossible to have such a stringent limitation in software development. But many interviewers think this is a good ability to test candidates divergent thinking, and divergent thinking can reflect the breadth of the candidate's knowledge, as well as the depth of understanding of programming-related technology.

Analysis: Usually seek 1+2+ +n has three conventional ideas: (1) loops, (2) recursion (the use of if statements or conditional judgement statements to determine whether to continue recursion or terminate recursion); (3) Use formula N (n+1)/2. These three general scenarios are not available because of the limitations of the topic. So how do you solve the problem with unconventional solutions? Below provides two kinds of problem-solving ideas, corresponding to four kinds of solutions, in essence or using a circular or recursive thinking, but the wording changed.

One way to solve problems: circular angle

Basic idea: Thinking from the circular angle. The loop just lets the same code repeat n times, and we can do it without the for and while. For example, the following constructor method is used.

Solution One: Using the constructor function to solve

Specific scenario: We first define a type, and then create an instance of n that type, then the constructor for that type will be determined to be called n times. We can put the additive-related code into the constructor.

Implementation code:

Class temp  
{public  
:  
    temp () {++n; Sum = N;  
    static void Init () {N = 0; Sum = 0;  
    static unsigned int getsum () {return Sum;}  
      
Private:  
    static unsigned int N;  
    static unsigned int Sum;  
      
unsigned int temp::n = 0;  
unsigned int temp::sum = 0;  
      
unsigned int sum_solution1 (unsigned int n)  
{  
    temp::init ();  
      
    Temp *temp = new Temp[n];  
    delete []temp;  
    temp = NULL;  
      
    return Temp::getsum ();  
}

Two ways to solve problems: recursive perspective

Basic idea: Thinking from the point of view of recursion. Depending on the problem, we cannot judge the condition of a recursive termination in a function, so we may define two functions: one function acts as a recursive role in the original recursive function, and another function handles the recursive termination, and what we need to do is to select one of the two functions in two. From two, it's natural to think of a Boolean variable called the first function when the value is true (1), and the second function is called when the value is False (0). So how do you convert a numeric variable N to a Boolean variable? The solution is to do two consecutive inverse operation of N, that is!! n, so nonzero n is converted to true,0 to false.

There are three solutions based on this idea.

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.