The solution method of C language seeking 1+2+...+n _c language

Source: Internet
Author: User
Title: Seek 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).
Analysis:This problem has little practical significance, because there is no such a perverse limitation in software development. But this problem can effectively examine divergent thinking ability, and divergent thinking ability reflects the depth of understanding of programming-related technology.
In addition to the 1+2+...+N formula N (n+1)/2, there is no outside circulation and recursion in the two ways. Because the use of for and while is explicitly restricted, the loop is no longer available. Similarly, recursive functions need to use if statements or conditional judgments to determine whether to continue recursion or terminate recursion, but now the topic has not allowed the use of these two statements.
We still have a circle around the loop. The loop just lets the same code execute n times, and we can do it without the for and while. For example, to define a class, we new an array containing n of this type of element, then the constructor of the class will be determined to be called n times. We can put the code that needs to be executed into the constructor. The following code is based on this idea:
Copy Code code as follows:

Class Temp
{
Private
static int N;
static int Sum;
Public
Temp () {+ + N;    Sum = N; }
static void Reset () {N = 0; Sum = 0; }
static int getsum () {return Sum; }
};
int temp::n = 0; The value of a static member is the same for all objects. Static members can be initialized, but can only be initialized outside of the class body.
int temp::sum = 0;
int solution1_sum (int n)
{
Temp::reset ();
Temp *a = new Temp[n];
delete []a;
A = 0;
return Temp::getsum ();
}

We can also make a fuss around recursion. Since we can't judge whether we should terminate recursion, we might as well define two functions. One function acts as the role of a recursive function, and another function handles the termination of recursion, and all we need to do is to select one of the two functions in two. From two Select one we naturally think of Boolean variables, such as ture (1), when the first function is called, False (0) when the second function is called. The problem now is to convert the numeric variable N to a Boolean value. If you do two consecutive inverse operations on N, that is!! N, then the Non-zero n conversion to true,0 is converted to false. With the above analysis, let's look at the following code:
Copy Code code as follows:

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; }
};
int solution2_sum (int n)
{
A A;

Array[0] = &a;
ARRAY[1] = &b;
int value = Array[1]->sum (n);
return value;
}

This method uses virtual function to realize the choice of function. Executes the function b::sum when n is not zero, and executes A::sum when n is 0 o'clock. We can also use an array of function pointers directly, which may be more straightforward:
Copy Code code as follows:

typedef int (*FUN) (int);
int solution3_f1 (int i)
{
return 0;
}
int solution3_f2 (int i)
{
Fun f[2]={solution3_f1, solution3_f2};
Return i+f[!! I] (i-1);
}

In addition, we can have the compiler help us to do a recursive operation, such as the following code:
Copy Code code as follows:

Template <int n> struct solution4_sum
{
Enum Value {n = solution4_sum<n-1>::n + n};
};
Template <> struct solution4_sum<1>
{
Enum Value {N = 1};
};

Solution4_sum<100>::n is the result of 1+2+...+100. When the compiler sees solution4_sum<100>, it is the template class
Solution4_sum generates this type of code with parameter 100. However, a type with a parameter of 100 needs to be given a type with a parameter of 99 because of the solution4_sum<100>::n=solution4_sum<99>::n+100. This process recursively goes back to the type with the parameter 1, and as the type has been explicitly defined, the compiler does not need to generate, and recursion compiles to this end. Because this process is done during compilation, the requirement for input n must be determined during compilation and cannot be entered dynamically. This is the biggest drawback of the method. And the compiler has a limit to the recursive depth of recursive code, which requires that n not be too large.

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.