Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5.  
  6. int main()
  7. {
  8.     int iStonesValue = 0;
  9.     scanf_s("%d", &iStonesValue);
  10.  
  11.     int iTmp,iSum = 0;
  12.     int *heap = (int*) malloc (iStonesValue * sizeof(int));
  13.     for (int i = 0; i < iStonesValue; i++)
  14.     {
  15.         scanf_s("%d", &heap[i]);
  16.         iSum += heap[i];
  17.     }
  18.     for (int i = 0; i < iStonesValue-1; i++)
  19.     {
  20.         if(heap[i] > heap[i+1])
  21.         {
  22.             iTmp = heap[i];
  23.             heap[i] = heap[i + 1];
  24.             heap[i + 1] = iTmp;
  25.         }
  26.     }
  27.  
  28.     int iFirstHeapWeight = abs(iSum - heap[iStonesValue - 1]);
  29.     int iSecondHeapWeight = heap[iStonesValue - 1];
  30.     int iHeapWeightDifference = abs(iFirstHeapWeight - iSecondHeapWeight);
  31.     for (int i = 0; i < iStonesValue-1; i++)
  32.     {
  33.         if (iHeapWeightDifference >= abs(iFirstHeapWeight - iSecondHeapWeight))
  34.         {
  35.             iHeapWeightDifference = abs(iFirstHeapWeight - iSecondHeapWeight);
  36.         }
  37.         iSecondHeapWeight += heap[i];
  38.         iFirstHeapWeight = abs(iSum - iSecondHeapWeight);
  39.     }
  40.     printf("%d", iHeapWeightDifference);
  41.     free(heap);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement