抛硬币游戏模拟

main.c

#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;
        }

    
    
return 0;
}


int  __GS_queue_free()
{
    
if (x->container != NULL)
        free(x
->container);
    x
->container = NULL;
    
if (x != NULL)
        free(x);
    x 
= NULL;
    
    
return 0;
}


int  __GS_queue_in( int   in )
{
    
int _out = -1;
    
if (x->cursor == 0)
        
{
        x
->container[x->cursor] = in;
        x
->cursor = 1;
        }

    
else if (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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值