Csdn's ideas and help for splitting the candy Algorithm

Source: Internet
Author: User

I did a candy-sharing question on csdn last night. I did not have a problem, but after I submitted the answer, I failed and prompted

Your program runs normally and outputs results, but the answer is incorrect.
The output result of your program does not match the output result in the test data.

Let me talk about my own ideas first, and then paste my code out. You can help me to see what went wrong or share some better ideas.

The question is like this: Address: http://student.csdn.net/mcs/programming_challenges second question

There are n Children standing in a row (numbers from 0 to n-1). Each child has a rating value, which is stored in the ratings array. The teacher needs to allocate candy to them. Each child needs at least one candy. For any two adjacent children, I and I + 1, the value of rating must be smaller than the value of rating. The value of rating must be equal to the value of rating ). Calculate the minimum number of sweets required to complete the allocation. Input Format: multiple groups of data. The first row of each group is a positive integer n. In the next n rows, each row has a positive integer, indicating the rating value of each child. All integers cannot exceed 100000. Output Format: a row of data in each group, including a positive integer, indicating the number of required sweets.

 

My idea is to traverse the children from 0 and find the minimum number of Sweets that the children can get at each time;

The preceding position can be compared once. If the value is greater than the Rating Value of the previous child, the number of Sweets that the previous child expected is + 1, if the value is less than or equal to the rating value of the first child, the expected number of Sweets is automatically changed to 1;

Set the Temporary Variable corresponding to the expected number of sweets to be compared later, and then start to compare later. If the rating value of the later child is smaller than the Rating Value of the current child, then, the Temporary Variable + 1 of the number of Sweets that the current child deserves will continue to compare with the later child. If it is decreasing, it will continue to increase by 1 until the decline ends;

Compare the temporary variable with the expected number of sweets, and take the maximum value of the two, that is, the minimum number of Sweets that the current children deserve.

In this way, the minimum number of sweets each time a child receives is obtained.

The Code is as follows:

1 static void main (string [] ARGs) 2 {3 console. writeline ("Enter n to jump out of the loop, or start the game with other characters"); 4 While (console. Readline ()! = "N") 5 {6 getmin (); 7 console. writeline ("Enter n to jump out of the loop, or other characters to continue the game"); 8} 9 10} 11 static void getmin () 12 {13 // console. writeline ("enter an integer"); 14 15 string input = console. readline (); 16 int inputnum; 17 while (! Int. tryparse (input, out inputnum) 18 {19 // console. writeline ("enter an integer"); 20 input = console. readline (); 21} 22 if (inputnum = 0) 23 {24 console. writeline ("0"); 25 return; 26} 27 28 29 int [] arys = new int [inputnum]; 30 // random Rm = new random (); 31 // For (INT I = 0; I <inputnum; I ++) 32 // {33 // arys [I] = RM. next (1, 15); 34 // console. write (arys [I] + ""); 35 //} 36 // console. writeline (); 37 38 int TMP; 39 for (I NT I = 0; I <inputnum; I ++) 40 {41 input = console. Readline (); 42 while (! Int. tryparse (input, out TMP) | TMP> 100000 | TMP <= 0) 43 {44 console. writeline ("enter a positive integer up to 100000"); 45 input = console. readline (); 46} 47 arys [I] = TMP; 48} 49 50 int sum = 0; 51 int num = 1; 52 Int J; 53 // traverse every child 54 for (INT I = 0; I <arys. length; I ++) 55 {56 J = I; 57 // obtain the expected minimum number of sweets by forward comparison. If (j> = 1 & arys [J]> arys [J-1]) 59 {60 num ++; 61} 62 else63 {64 num = 1; 65} 66 // start future comparison and find the expected minimum number of sweets 67 int next = 1; 68 while (j <arys. length-1 & arys [J]> arys [J + 1]) 69 {70 J ++; 71 next ++; 72} 73 // compare the results of forward comparison and backward comparison, take the maximum value of the two as the expected minimum number of sweets 74 num = num> next? Num: Next; 75 // console. writeline ("Num:" + num); 76 sum + = num; 77} 78 console. writeline ("minimum number of expected sweets" + sum); 79}
View code

 

Csdn's ideas and help for splitting the candy Algorithm

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.