fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*#define XMALLOC */
  5. #if defined XMALLOC
  6. #include "xmalloc.h"
  7. #else
  8. #define xmalloc(x, y) malloc((x))
  9. #define xfree(x, y) free((x))
  10. #define xrealloc(x, y, z) realloc((x), (y))
  11. #define xmallocdump()
  12. #endif
  13. #define ID_VECTOR 1001
  14. #define ID_ARRAY 1002
  15.  
  16.  
  17. #define T int
  18. typedef struct {
  19. T *array;
  20. int size;
  21. } Vector;
  22.  
  23. int init_vector(Vector **v, int size) {
  24. if ((*v = xmalloc(sizeof(Vector), ID_VECTOR)) == 0)
  25. return 0;
  26. if (((*v)->array = xmalloc(sizeof(T) * size, ID_ARRAY)) != 0) { /* OK */
  27. (*v)->size = size;
  28. return 1;
  29. }
  30. return 0;
  31. }
  32.  
  33. int substitute_vector(Vector *v, int index, T value) {
  34. int n, *alloc;
  35. if (index < v->size) {
  36. (v->array)[index] = value;
  37. return 1;
  38. }
  39. n = index * 2;
  40. if ((alloc = xrealloc(v->array, sizeof(T) * n, ID_ARRAY)) != 0) { /* OK */
  41. v->array = alloc;
  42. v->size = n;
  43. (v->array)[index] = value;
  44. return 1;
  45. }
  46. return 0;
  47. }
  48.  
  49. T refer_vector(Vector *v, int index) {
  50. if (index < v->size)
  51. return (v->array)[index];
  52. perror("out of bounds in refering, aborted\n");
  53. exit(1);
  54. }
  55.  
  56. void release_vector(Vector **v) {
  57. (*v)->size = 0;
  58. xfree((*v)->array, ID_ARRAY);
  59. xfree(*v, ID_VECTOR);
  60. *v = 0;
  61. }
  62.  
  63. #define N 1
  64. int main() {
  65. Vector *a;
  66. int last, i, bids, input, ferr;
  67.  
  68. if (!init_vector(&a, 10)) { perror("memory full(init), aborted.\n"); exit(1); }
  69. restart:
  70. last = 3;
  71. ferr = 0;
  72. for (i = 0; i < last; i++)
  73. if(!substitute_vector(a, i, i + 1)) {
  74. ferr = 1;
  75. break;
  76. }
  77. if (ferr) { perror("memory full(subst4init), aborted.\n"); exit(1); }
  78. for (;;) {
  79. ferr = 0;
  80. for (i = 0; i < last; i++)
  81. printf("[%d]%d, ", i, refer_vector(a, i));
  82. putchar('\n');
  83. bids = refer_vector(a, 0) + refer_vector(a, last - 1);
  84. printf("bids: %d\n", bids);
  85. printf("win or loss? (win:positive, loss:zero) : "); scanf("%d", &input);
  86. if (input < 0)
  87. break;
  88. if (input) {
  89. for (i = 0; i < last - 1; i++)
  90. if (!substitute_vector(a, i, refer_vector(a, i + 1))) { ferr = 1; }
  91. last -= 2;
  92. } else {
  93. if (!substitute_vector(a, last, bids)) { ferr = 1; }
  94. last++;
  95. }
  96. if (ferr)
  97. break;
  98. if (last < 2) {
  99. printf("go for the initial step.\n");
  100. goto restart;
  101. }
  102. }
  103. if (ferr) { perror("memory full(subst4init), aborted.\n"); exit(1); }
  104. release_vector(&a);
  105. xmallocdump();
  106. return 0;
  107.  
  108. }
  109.  
  110. /* end */
  111.  
Success #stdin #stdout 0s 2384KB
stdin
0
1
0
1
-1
stdout
[0]1, [1]2, [2]3, 
bids: 4
win or loss? (win:positive, loss:zero) : [0]1, [1]2, [2]3, [3]4, 
bids: 5
win or loss? (win:positive, loss:zero) : [0]2, [1]3, 
bids: 5
win or loss? (win:positive, loss:zero) : [0]2, [1]3, [2]5, 
bids: 7
win or loss? (win:positive, loss:zero) : go for the initial step.
[0]1, [1]2, [2]3, 
bids: 4
win or loss? (win:positive, loss:zero) :