#include <stdio.h>
#include <stdlib.h>
/*#define XMALLOC */
#if defined XMALLOC
#include "xmalloc.h"
#else
#define xmalloc(x, y) malloc((x))
#define xfree(x, y) free((x))
#define xrealloc(x, y, z) realloc((x), (y))
#define xmallocdump()
#endif
#define ID_VECTOR 1001
#define ID_ARRAY 1002
#define T int
typedef struct {
T *array;
int size;
} Vector;
int init_vector(Vector **v, int size) {
if ((*v = xmalloc(sizeof(Vector), ID_VECTOR)) == 0)
return 0;
if (((*v)->array = xmalloc(sizeof(T) * size, ID_ARRAY)) != 0) { /* OK */
(*v)->size = size;
return 1;
}
return 0;
}
int substitute_vector(Vector *v, int index, T value) {
int n, *alloc;
if (index < v->size) {
(v->array)[index] = value;
return 1;
}
n = index * 2;
if ((alloc = xrealloc(v->array, sizeof(T) * n, ID_ARRAY)) != 0) { /* OK */
v->array = alloc;
v->size = n;
(v->array)[index] = value;
return 1;
}
return 0;
}
T refer_vector(Vector *v, int index) {
if (index < v->size)
return (v->array)[index];
perror("out of bounds in refering, aborted\n"); }
void release_vector(Vector **v) {
(*v)->size = 0;
xfree((*v)->array, ID_ARRAY);
xfree(*v, ID_VECTOR);
*v = 0;
}
#define N 1
int main() {
Vector *a;
int last, i, bids, input, ferr;
if (!init_vector
(&a
, 10)) { perror("memory full(init), aborted.\n"); exit(1); } restart:
last = 3;
ferr = 0;
for (i = 0; i < last; i++)
if(!substitute_vector(a, i, i + 1)) {
ferr = 1;
break;
}
if (ferr
) { perror("memory full(subst4init), aborted.\n"); exit(1); } for (;;) {
ferr = 0;
for (i = 0; i < last; i++)
printf("[%d]%d, ", i
, refer_vector
(a
, i
)); bids = refer_vector(a, 0) + refer_vector(a, last - 1);
printf("win or loss? (win:positive, loss:zero) : "); scanf("%d", &input
); if (input < 0)
break;
if (input) {
for (i = 0; i < last - 1; i++)
if (!substitute_vector(a, i, refer_vector(a, i + 1))) { ferr = 1; }
last -= 2;
} else {
if (!substitute_vector(a, last, bids)) { ferr = 1; }
last++;
}
if (ferr)
break;
if (last < 2) {
printf("go for the initial step.\n"); goto restart;
}
}
if (ferr
) { perror("memory full(subst4init), aborted.\n"); exit(1); } release_vector(&a);
xmallocdump();
return 0;
}
/* end */
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8qI2RlZmluZSBYTUFMTE9DICovCiNpZiBkZWZpbmVkIFhNQUxMT0MKI2luY2x1ZGUgInhtYWxsb2MuaCIKI2Vsc2UKI2RlZmluZSB4bWFsbG9jKHgsIHkpIG1hbGxvYygoeCkpCiNkZWZpbmUgeGZyZWUoeCwgeSkgZnJlZSgoeCkpCiNkZWZpbmUgeHJlYWxsb2MoeCwgeSwgeikgcmVhbGxvYygoeCksICh5KSkKI2RlZmluZSB4bWFsbG9jZHVtcCgpCiNlbmRpZgojZGVmaW5lIElEX1ZFQ1RPUiAxMDAxCiNkZWZpbmUgSURfQVJSQVkgIDEwMDIKCgojZGVmaW5lIFQgaW50CnR5cGVkZWYgc3RydWN0IHsKICBUICphcnJheTsKICBpbnQgc2l6ZTsKfSBWZWN0b3I7CgppbnQgaW5pdF92ZWN0b3IoVmVjdG9yICoqdiwgaW50IHNpemUpIHsKICBpZiAoKCp2ID0geG1hbGxvYyhzaXplb2YoVmVjdG9yKSwgSURfVkVDVE9SKSkgPT0gMCkKICAgIHJldHVybiAwOwogIGlmICgoKCp2KS0+YXJyYXkgPSB4bWFsbG9jKHNpemVvZihUKSAqIHNpemUsIElEX0FSUkFZKSkgIT0gMCkgeyAvKiBPSyAqLwogICAgKCp2KS0+c2l6ZSA9IHNpemU7CiAgICByZXR1cm4gMTsKICB9CiAgcmV0dXJuIDA7Cn0KCmludCBzdWJzdGl0dXRlX3ZlY3RvcihWZWN0b3IgKnYsIGludCBpbmRleCwgVCB2YWx1ZSkgewogIGludCBuLCAqYWxsb2M7CiAgaWYgKGluZGV4IDwgdi0+c2l6ZSkgewogICAgKHYtPmFycmF5KVtpbmRleF0gPSB2YWx1ZTsKICAgIHJld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MDsKCn0KCi8qIGVuZCAqLwo=
[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) :