Topic:
There are n children, they have a rating value, children stand in a row, this time the rating value in order into the array, the protagonist to give n small children hair candy, but also stingy, want to try the least candy is issued. To meet the following two conditions:
1, each child must have a candy at least
2, have a higher rating value of children than their own side of the children get more candy
such as: ratings = {1,2,2,2,3,2,1}; We have to allocate at least the candy Candys = {1,2,1,1,3,2,1} = 11 Sweets
input:{1,2,2,2,3,2,1};
Expected:11
Input: [1,2,4,4,3]
Expected:9
input:[1,2,2]
Expected:4
Program:
#include <stdio.h> #include <stdlib.h> #include <memory.h>/************************************** * Function Name: Adjustment * Function Description: Three cycles complete all candies allocation * parameter definition: Ratings rating value array pointer * Len number leader
Degree * return value: Returns the total number of sweets ***********************************************************/int adjustment (int *ratings, int len) {
int num = 0;
int *candies = (int *) malloc (sizeof (int) *len);
if (NULL = = ratings) return 0;
if (Len <= 1) return len;
if (NULL = = candies) return 0;
Each person assigns a candy for (int i = 0; i < len; i++) candies[i] = 1; Iterate back for (int i = 1; i < Len; i++) {if (Ratings[i] > Ratings[i-1] && candies[i] <=
CANDIES[I-1]) candies[i] = candies[i-1] + 1; (int i = len-2 i >= 0; i--) {if (Ratings[i] > ratings[i + 1] && Candi Es[i] <= candies[i + 1]) Candies[i = CandiEs[i + 1] + 1;
//Results Add printf ("The Candies:");
for (int i = 0; i < len; i++) {num + = Candies[i];
printf ("%d", candies[i]);
printf ("\ n");
Free (candies);
Candies = NULL;
return num; /*********************************************************** * Function Name: test_adjustment * Function Description: Test case * Parameter definition: ************
/void Test_adjustment1 () {int tratings[] = {2, 3};
int tlen = sizeof (tratings)/sizeof (int);
printf ("The ratings:");
for (int i = 0; i < Tlen i++) printf ("%d", tratings[i]);
printf ("\ n");
printf ("The candies number is%d!\n", Adjustment (Tratings, Tlen));
} void Test_adjustment2 () {int tratings[] = {1, 2, 3, 4, 5, 4, 3, 2, 1};
int tlen = sizeof (tratings)/sizeof (int);
printf ("The ratings:");
for (int i = 0; i < Tlen i++) printf ("%d", tratings[i]);
printf ("\ n"); printf ("The Candies number is%d!\n ", Adjustment (Tratings, Tlen));
} void Test_adjustment3 () {int tratings[] = {1};
int tlen = sizeof (tratings)/sizeof (int);
printf ("The ratings:");
for (int i = 0; i < Tlen i++) printf ("%d", tratings[i]);
printf ("\ n");
printf ("The candies number is%d!\n", Adjustment (Tratings, Tlen));
} void Test_adjustment4 () {int tratings[] = {2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
int tlen = sizeof (tratings)/sizeof (int);
printf ("The ratings:");
for (int i = 0; i < Tlen i++) printf ("%d", tratings[i]);
printf ("\ n");
printf ("The candies number is%d!\n", Adjustment (Tratings, Tlen)); /*********************************************************** * Function Name: main * Function Description: main function * Parameter definition: ************************
/int main () {test_adjustment4 ();
return 0;
}