Beauty of programming-only additional interview questions

Source: Internet
Author: User

The first response to the question is DP, which can be calculated using the previous values. The recursive formula is as follows: F (n) = f (n-I) + I

You can add appropriate pruning, but for 64-bit integers, there is certainly not enough storage space to give up.

 

After consideration, the formula is deduced:

 

Num = Σ I (BG <= I <= END) = (bg + end) (end-bg + 1)/2

 

In the above formula, BG and end are respectively the start position and the end position in the summation formula, both of which are closed intervals.

Sub = end-bg + 1, add = bg + end

The preceding equation is converted to num = sub * Add/2, and the original proposition is converted to an appropriate interval problem.

After this formula is obtained, it is much easier to solve it.

 

Considering that sub cannot be greater than add or other information, it can be used for pruning. The final code for complexity of N ^ 2 is as follows:

// Only add interview questions <br/> # include <iostream> <br/> # include <cmath> <br/> using namespace STD; <br/> # DEFINE _ debug <br/> inline bool iseven (const Int & num) <br/>{< br/> return! (Num & (0x01); <br/>}< br/> inline void printresult (INT num, int CNT, int BG, int end) <br/> {<br/> cout <"no." <CNT <"group combination:/N" <num <"=" <BG <" +... + "; <br/> // For (INT I = BG; I <end; ++ I) <br/> // cout <I <" + "; <br/> cout <End <Endl; <br/>}< br/> // we can obtain the EQ. <br/> // num = (e-B + 1) (E + B) /2 <br/> // then use N ^ 2 algorithm to find suitable <br/> // E and B <br/> void slove (Int & num) <br/> {<br/> int sub = 2, // E-B + 1 <br/> Add = 2, // e + B <br/> boundery = 0, // up-boundery <br/> CNT = 0; <br/> If (iseven (Num) <br/>{// sub begin Form 3, cuz two numbers cant obtain even sum <br/> sub = 3; <br/> Add = 3; <br/>}< br/> num <= 1; <br/> for (; sub <= add; ++ sub) <br/>{< br/> Add = sub; <br/> (; boundery <= num & add <= num; ++ add) <br/> {<br/> boundery = (sub * Add ); <br/> If (boundery = num) {<br/> int BG = (add-sub + 1)/2), <br/> End = (sub + Add-1)/2); <br/> If (! BG | BG = end | add! = Bg + end) <br/> continue; <br/> ++ CNT; <br/> printresult (Num> 1, CNT, BG, end ); <br/> break; <br/>}< br/> boundery = 0; <br/>}< br/> If (! CNT) <br/> cout <"not found! "<Endl; <br/>}< br/> int main () <br/>{< br/> int num; <br/> while (CIN> num) <br/>{< br/> slove (Num); <br/>}< br/> return 0; <br/>}

 

The change program is correct under the test of small data, but not for big data.

 

Because the algorithm itself still has defects, it is very slow to use the N ^ 2 algorithm when n is large. How can this problem be solved?

In fact, after reading the output results, we will have a specific idea:

The number of integers is summed up to traverse N. However, we still need to sum the range of N. here we can use the summation formula to solve the problem, therefore, the algorithm is O (n.

The new algorithm is as follows:

// Only add interview questions <br/> # include <iostream> <br/> # include <cmath> <br/> using namespace STD; <br/> # DEFINE _ debug <br/> inline bool iseven (const Int & num) <br/>{< br/> return! (Num & (0x01); <br/>}< br/> inline bool isodd (const Int & num) <br/>{< br/> return (Num & (0x1); <br/>}< br/> inline void printresult (INT num, int N, int CNT, int BG, int end) <br/> {<br/> cout <"no." <CNT <"group combination" <n <"digits are added to/t" <num <"=" <BG <"+... + "; <br/> // For (INT I = BG; I <end; ++ I) <br/> // cout <I <" + "; <br/> cout <End <Endl; <br/>}< br/> inline int sum (int bg, int end) <br/> {<br/> return (bg + end) * (end- Bg + 1); <br/>}< br/> // we can obtain the EQ. <br/> // num = (e-B + 1) (E + B) /2 <br/> // then use N ^ 2 algorithm to find suitable <br/> // E and B <br/> void slove2 (const Int & num) <br/> {<br/> int n = 3; // If num is an odd number, at least two numbers are added, and the even number must be at least three. <br/> int CNT = 0; <br/> If (isodd (Num) <br/>{< br/> If (Num <1 = sum (Num/2, num/2 + 1) <br/>{< br/> + CNT; <br/> printresult (Num, 2, CNT, num/2, num/2 + 1); <br/>}< br/> int upper = (Num/n + 1)> 3? (Num/n + 1): 3; <br/> for (INT I = N; I <= upper; ++ I) <br/>{< br/> int BG = Ceil (double) num/I)-(I/2.0 )); <br/> int end = floor (double) num/I) + (I/2.0); </P> <p> If (! BG) <br/> break; <br/> If (end-bg + 1! = I) <br/> continue; <br/> If (Num <1 = sum (BG, end )) <br/>{< br/> ++ CNT; <br/> printresult (Num, I, CNT, BG, end ); <br/>}< br/> If (! CNT) <br/> cout <"not found! "<Endl; <br/>}< br/> int main () <br/>{< br/> int num; <br/> while (CIN> num) <br/>{< br/> slove2 (Num); <br/>}< br/> return 0; <br/>}

 

The new algorithm is tested correctly in small data. If you have any questions, you are welcome to discuss it.

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.