Casual association of a Baidu star Programming Competition question · (2)

Source: Internet
Author: User

Baidu star is the world's largest Chinese search engine. Baidu holds a high-level Program Design Competition for Chinese college students and programmers. All his questions are algorithm questions.

Although I am a. Net programmer, I love algorithms after work. I think this question is a bit interesting, so I will share it with you. I think of two ways to provide you with an open mind.

Solution 2 is introduced below.

Solution 2: Small-sized amplification. Small and large. First, let's talk about my analysis ideas.

The first step is to judge whether I is no less than I/2 and then use this loop.

Step 2: Determine whether the value accumulation in this range is equal to a specific value.

Step 3: Output

Step 4: output the value of none if no value exists.

Now let's look at the corresponding source code.

1 Console. writeline ("enter a positive integer"); 2 int inputnum = convert. toint32 (console. readline (); 3 // indicates whether consecutive values are identified; 4 bool flag = false; 5 // the corresponding counter variable 6 int I = 1, j = 2, sum = 0; 7 // 8 while (I <inputnum/2) 9 {10 sum = add (I, j); 11 while (sum! = Inputnum & I <inputnum/2 + 1) 12 {13 if (sum> inputnum) 14 I ++; 15 else16 J ++; 17 sum = add (I, j); 18} 19 // print the corresponding loop 20 for (int K = I; k <= J & J> I; k ++) 21 {22 23 console. write (k); 24 25 console. write (""); 26} 27 I ++; 28 console. writeline (); 29} 30 31 if (! Flag) 32 {33 console. writeline ("NONE"); 34} 35 console. readkey (); 36 // calculate the accumulated algorithm 37 static int add (int m, int N) 38 {39 int sum = 0; 40 for (INT I = m; I <= N; I ++) 41 {42 sum + = I; 43} 44 return sum; 45}

The function of this algorithm is to see whether it is equal through loops.

He is different from the first algorithm. The first method is to subtract it from it to see if it can get 0. The second method is used to accumulate the algorithm to see if it is equal. And there are two pointer variables scanning each other.

All in all, these two algorithms have their own merits.

 

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.