#include <time.h> #include <stdlib.h> #include "GS_queue.h" int take_bit() ...{ srand((unsigned)time(0)); return rand() %2; } int finished(int n) ...{ int _i, _j, _times, _x1; __GS_queue_init(n -1); for (_times =0; ; ++_times) ...{ _i = take_bit(); if (_times <=1) goto next; else...{ for (_j =0; _j < n -1; ++_j) ...{ _x1 = __GS_queue_at(_j); if (_x1 ==0) break; } if (_j == n -1) break; } next: __GS_queue_in(x, _i); } __GS_queue_free(x); return _times +1; } int main() ...{ int _i = finished(3); printf("%d", _i); }
GS_queue.h
#ifndef __GS_QUEUE_H__ #define __GS_QUEUE_H__ typedef struct ...{ int size; int cursor; int* container; } __GS_Queue; __GS_Queue* x; int __GS_queue_init(int); int __GS_queue_free(); int __GS_queue_in(int); int __GS_queue_out(); int __GS_queue_at(int); #endif
GS_queue.c
#include <stdlib.h> #include "GS_queue.h" int __GS_queue_init(int size) ...{ int _i; x = (__GS_Queue*)malloc(sizeof(__GS_Queue)); if (x == NULL) return-1; x->cursor =0; x->size = size; x->container = (int*)malloc(size *sizeof(int)); if (x == NULL) return-1; for (_i =0; _i < size; ++_i) ...{ x->container[_i] =-1; } return0; } int __GS_queue_free() ...{ if (x->container != NULL) free(x->container); x->container = NULL; if (x != NULL) free(x); x = NULL; return0; } int __GS_queue_in(intin) ...{ int _out =-1; if (x->cursor ==0) ...{ x->container[x->cursor] =in; x->cursor =1; } elseif (x->cursor < x->size) ...{ x->container[x->cursor] =in; x->cursor +=1; } else...{ _out = __GS_queue_out(x); x->cursor +=1; x->container[x->cursor] =in; } return _out; } int __GS_queue_out() ...{ int _out = x->container[0]; int _i; for (_i =0; _i < x->cursor; ++_i) ...{ x->container[_i] = x->container[_i +1]; } x->cursor -=1; return _out; } int __GS_queue_at(int i) ...{ return x->container[i]; }