Advertisement
Guest User

Untitled

a guest
Aug 15th, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAXSTONES 20
  3. int main(void) {
  4.     unsigned int stones[MAXSTONES] = {0};
  5.     unsigned int pile1 = 0, pile2 = 0;
  6.     int stoneCount = 0;
  7.     int weightDifference = 0;
  8.     int i;
  9.    
  10.     char contMoving = 1;
  11.     unsigned int biggestStone;
  12.    
  13.     fscanf(stdin, "%d", &stoneCount);
  14.    
  15.     for(i = 0; i < stoneCount; i++) {
  16.         fscanf(stdin, "%u", &stones[i]);
  17.         pile1 += stones[i];
  18.     }
  19.    
  20.  
  21.     while(contMoving) {
  22.         weightDifference = pile1 - pile2;
  23.        
  24.         if(weightDifference != 0) {
  25.             biggestStone = 0;
  26.             for(i = 0; i < stoneCount; i++) {
  27.                 if(stones[i] < weightDifference && stones[i] > biggestStone) {
  28.                     if((pile1 - stones[i]) >= (pile2 + stones[i])) {
  29.                         biggestStone = stones[i];
  30.                     }
  31.                 }
  32.             }
  33.            
  34.             if(biggestStone != 0) {
  35.                 pile1 -= biggestStone;
  36.                 pile2 += biggestStone;
  37.             } else {
  38.                 contMoving = 0;
  39.                 break;
  40.             }
  41.            
  42.         } else {
  43.             contMoving = 0;
  44.             break;
  45.         }
  46.     }
  47.    
  48.     fprintf(stdout, "%u\n", weightDifference);
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement