/*
    第2種情報処理技術者試験    平成6年度・春期・午後 問18
                                BohYoh Shibata PREPARATION
*/

#define OK  0
#define NG  1
int uketsuke(int);
int hanbai(int);
int otsuri(int, int *, int *, int *);
void henkyaku(void);
static int c10 = 100;                       /* 10円硬貨の枚数 */
static int c50 = 100;                       /* 50円硬貨の枚数 */
static int c100 = 0;                        /* 100円硬貨の枚数 */
static int shina[4] = {120, 250, 400, 520}; /* 品物の値段(A,B,C,D) */
static int ksum = 0;                        /* 投入金額 / 残金 */

int uketsuke(int type) /* type: 硬貨種別(10円=10,50円=50,100円=100) */
{
    switch (type) {
      case  10:
        c10++;
        break;
      case  50:
        c50++;
        break;
      case  100:
        c100++;
        break;
      default:
        return NG;
    }
    ksum += type ;
    return OK;
}

int hanbai(int sid)  /* sid: 品物種別(A=0,B=1,C=2,D=3) */
{
    int n10, n50, n100;

    if (ksum  >=  shina[sid]) {
        if (otsuri(ksum-shina[sid],&n10,&n50,&n100) == OK) {
            ksum -= shina[sid] ;
            return OK;
        }
    }
    return NG;
}

int otsuri(int gaku, int *n10, int *n50, int *n100)
            /* gaku: おつり金額, n10~n100: 硬貨枚数 */
{
    int cnt;

    cnt =  gaku / 100 ;

    if (c100 < cnt) cnt = c100;
    *n100 =  cnt ;

    gaku -= cnt*100;
    cnt = gaku/50;
    if (c50 < cnt) cnt = c50;
    *n50 =  cnt ;

    gaku -= cnt*50;
    if ( c10 >= gaku / 10 ) {
        *n10 = gaku/10;
        return OK;
    }
    else
        return NG;
}

void henkyaku(void)
{
    int n10, n50, n100;

    otsuri(ksum, &n10, &n50, &n100);    /* 返却硬貨枚数の計算 */
    c10 -= n10;
    c50 -= n50;
    c100 -= n100;
    ksum = 0;
}