Question
C Programming There are N candy boxes, and each box has Ai candy. The candy box will eventually be combined into one, combining only two
C Programming There are N candy boxes, and each box has Ai candy. The candy box will eventually be combined into one, combining only two boxes at a time and combining the two boxes with the largest number of candy in each step. If the number of boxes is an odd number, two of them are combined except one box with the smallest number of candies. For example, given N = 6 and 1, 2, 3, 4, 5, 6, the first stage is 11,7,3, the second stage is 18, and the third stage is 21. Exit because the boxes are merged into one. (** Candy boxes are given in ascending order based on the number of candies **)
Input: The first line is given the integer N (2
If N is 6, the number of candy boxes is 6, so malloc is used to assign a dynamic array of size 6. Then, when the number of candy in the candy box is given as 1,2,3,4,5,6, the first step is obtained as follows.
In the first step, when two pairs are paired, the number of candy boxes in the next step becomes three, so the pointer of the next step is allocated and stored as a dynamic array of size 3, and this process is repeated to produce a dynamic array of size 1 do.
Please help me.
Can't get the right answer when input three numbers.
Thanks in advance.
My code:
#include
void sort(int *a, int n)/*{{{*/ { int temp, i, j; for (i = 0; i
//if size is odd place last element of prev in last of next if (size % 2) { next_pointer[next_size - 1] = prev_pointer[size - 1]; int k = 0, i; for (i = 0; i
sort(next_pointer, next_size); } else { int k = 0, i; for ( i = 0; i
sort(next_pointer, next_size); }
free(prev_pointer); return next_pointer; } /*}}}*/
int main()/*{{{*/ { int n, i; scanf("%d", &n);
int *a = (int *)malloc(sizeof(int) * n); for (i = 0; i
int prev_size = n; int new_size = (n / 2) + (n % 2);
while (new_size >= 1) { a = get_next_process(a, prev_size); for (i = 0; i
Prev pointer Next pointen mi3
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started