#include <stdio.h>
#include <stdlib.h>
#define T int
typedef struct {
T *array;
int size;
} Vector;
int init_vector(Vector *v, int size) {
if ((v
->array
= malloc(sizeof(T
) * size
)) != 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
= realloc(v
->array
, sizeof(T
) * n
)) != 0) { /* OK */ 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; free(v
->array
); }
#define N 10
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);
return 0;
}
/* end */
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCiNkZWZpbmUgVCBpbnQKdHlwZWRlZiBzdHJ1Y3QgewogIFQgKmFycmF5OwogIGludCBzaXplOwp9IFZlY3RvcjsKCmludCBpbml0X3ZlY3R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AgaWYgKGZlcnIpCiAgICAgIGJyZWFrOwogICAgaWYgKGxhc3QgPCAyKSB7CiAgICAgIHByaW50ZigiZ28gZm9yIHRoZSBpbml0aWFsIHN0ZXAuXG4iKTsKICAgICAgZ290byByZXN0YXJ0OwogICAgfQogIH0KICBpZiAoZmVycikgeyBwZXJyb3IoIm1lbW9yeSBmdWxsKHN1YnN0NGluaXQpLCBhYm9ydGVkLlxuIik7IGV4aXQoMSk7IH0KICByZWxlYXNlX3ZlY3RvcihhKTsKICByZXR1cm4gMDsKfQovKiBlbmQgKi8K
[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) :