Any natural number is decomposed into consecutive natural numbers and problems.

Source: Internet
Author: User

 

The day before yesterday, I learned that a friend went to Netease to apply for a job. I only encountered several troublesome algorithm problems in the written examination (mainly the difficulty during the examination) and told us how to discuss these problems together, any natural number N is decomposed into continuous natural numbers and the problem is one of them. This problem easily leads to a sequence whose length is k + 1 and must be equal to 0 ~ The sum of K plus k + 1 m, but to get an efficient algorithm, you must first find an ideal boundary condition to effectively reduce unnecessary searches, if the boundary is incorrect, a complete and correct solution is not obtained. At first, I always wanted to directly find the longest sequence to get its length. However, it was really a poor mathematical skill and I had to find another condition with a lower degree. This condition is: when the length of the current sequence K corresponds to 0 ~ If the sum of K is greater than the natural number N, the test is terminated. This principle is easy to understand, right?

The following code is used:

# Include <iostream>
# Include <cmath>

Using STD: cout;
Using STD: CIN;
Using STD: Endl;

Int main (void)
...{
Int n = 0;
Cout <"Enter N :";
While (! (CIN> N ))
...{
Cout <"input fail, Please retry! "<Endl;
Cout <"Enter N :";
Cin. Clear ();
}
Int K = 0;
Int M = N;
Int I = 0;
Int Big, small, pre_ I; // there are only 10 variables here to make the meaning of the Code clearer, which can be omitted.
Int J = 0; // This is mainly for testing and analysis convenience, and is not necessary.
Do
...{
Pre_ I = k = K + I;
M = (n-k)/(I + 1 );
If (n = m * (I + 1) + k)
...{
Big = m + I;
Small = m;
++ J;
Cout <"circle [" <I + 1 <"]; from" <small <"to" <big <Endl;
}
++ I;
} While (n> pre_ I + 1); // if n <pre_ I (the first I and), it indicates that the longest sequence is reached.
Cout <"circles [" <I <"] total" <j <Endl;
System ("pause ");
Return 0;
}

 

Below is Test 1:

It can be seen that the complexity of this number is very small and almost ideal. Then try another one:

Look, it's nearly doubled from the ideal 640 cycles. However, this gap does not increase monotonically.

However, the efficiency is still acceptable. On my core e6300 machine, it can be said that the output operation took a long time.

 

However ..... When N is close to the data type, the overflow problem is too serious. The remedy is to forcibly convert the data in the while loop condition to unsigned to increase the data limit. Although there is nothing wrong with mathematical theory, it is obviously not suitable for computer implementation. This reminds me of some people who think that algorithms can be studied without writing code or even understanding computer principles. Is this possible? Is there a theoretical error like the above algorithm? However, the actual conditions are limited. If you don't understand how data overflows, you won't be able to predict this kind of error in advance. The algorithm that God came up with for half a day can only be empty talk.

Another solution is to estimate the maximum length of the log sequence, and use this length as the cycle end condition. After analysis, the maximum length should be:

Int maxlength = POW (N * 2.0, 0.5) + 1;

The program is modified as follows:

# Include <iostream>
# Include <cmath>
Using STD: cout;
Using STD: CIN;
Using STD: Endl;
Int main (void)
...{
Int n = 0;
Cout <"Enter N :";
While (! (CIN> N ))
...{
Cout <"input fail, Please retry! "<Endl;
Cout <"Enter N :";
Cin. Clear ();
}
Int K = 0;
Int M = N;
Int I = 0;
Int Big, small, pre_m; // there are only 10 variables here to make the meaning of the Code clearer, which can be omitted.
Int J = 0; // This is mainly for testing and analysis convenience, and is not necessary.
Int maxlength = POW (N * 2.0, 0.5) + 1; // This is the longest sequence length we have estimated.
// Int maxlength = Log (double) N );
Cout <"maxlength =" <maxlength <Endl;
Do
...{
K = K + I;
// Pre_m = K;
M = (n-k)/(I + 1 );
If (n = m * (I + 1) + k)
...{
Big = m + I;
Small = m;
++ J;
Cout <"circle [" <I + 1 <"]; from" <small <"to" <big <Endl;
}
++ I;
///} While (unsigned) n> (unsigned) pre_m); // if n <pre_ I (first I and), it indicates that the longest sequence is reached
} While (I <maxlength );
Cout <"circles [" <I <"] total" <j <Endl;
System ("pause ");
Return 0;
}

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.