1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdint.h>

#define BAKET_SWAP(a,b) { uint32_t *work = *(a); *(a) = *(b); *(b) = work; }

void baket_sort( uint32_t **baket[] )
{
  int is_changed = 1;
  while( is_changed == 1 )
  {
    is_changed = 0;
    if( (*(baket[0])) < (*(baket[2])) ){ BAKET_SWAP(baket[0], baket[2]); is_changed = 1;}
    if( (*(baket[0])) < (*(baket[1])) ){ BAKET_SWAP(baket[0], baket[1]); is_changed = 1;}
    if( (*(baket[1])) < (*(baket[2])) ){ BAKET_SWAP(baket[1], baket[2]); is_changed = 1;}
  }
}

uint64_t play_game(uint32_t A, uint32_t B, uint32_t C)
{
  uint32_t *baket[] = {&A, &B, &C};
  uint64_t count = 0;

  while( 1 )
  {
    /* 水の量が均一になれば終わり */
    if( A == B )
    {
      printf("のこるのは C\n");
      count += A;
      break;
    }
    if( A == C )
    {
      printf("のこるのは B\n");
      count += A;
      break;
    }
    if( B == C )
    {
      printf("のこるのは B\n");
      count += B;
      break;
    }
    /* 昇順にバブルソート */
    baket_sort( &baket );
    (*(baket[0])) --; (*(baket[1])) --;
    (*(baket[2])) += 2;
    count ++;
  }
  return count;
}

int main(int argc, char *argv[])
{
  printf("ans = %lld\n", play_game(4,2,10));
  printf("ans = %lld\n", play_game(3,1,12));
  return 0;
}