結果

問題 No.842 初詣
コンテスト
ユーザー GinFizz1185
提出日時 2019-07-02 17:11:31
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,766 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 247 ms
コンパイル使用メモリ 38,252 KB
最終ジャッジ日時 2026-02-22 03:44:44
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<stdio.h>
#include<stdlib.h>

#define STR_LENGTH 23
#define NUM_INPUT 7
#define OK 1
#define NO -1

const static unsigned int coin_value[NUM_INPUT] = {500, 100, 50, 10, 5, 1};

void str_cut(char*, unsigned int*);
char judge_dating(unsigned int*);

int main(void){
  char judge_num = 0;
  char str[STR_LENGTH];
  unsigned int money[NUM_INPUT] = {0}; /* [0]:500エン玉の枚数 etc */

  fgets(str, sizeof(str), stdin);
  str_cut(str, money);

  judge_num = judge_dating(money);
  if(OK == judge_num){
    printf("YES\n");
  } else {
    printf("NO\n");
  }

  return 0;
}

/*
関数概要:渡された文字列を空白で分離し、数値に変換する
第1引数:文字列
第2引数:文字列から数値に変換して格納する配列
*/
void str_cut(char* str, unsigned int* money){
  int start_str = 0;
  int num = 0; //配列NO
  int i = 0;

  while('\0' != str[i]){
    if(' ' == str[i]){
      str[i] = '\0';
      money[num] = (unsigned int) atoi(&str[start_str]);
      ++num;
      ++i;
      start_str = i;
    }
    ++i;
  }

  money[num] = (unsigned int) atoi(&str[start_str]);

  return;
}

/*
関数概要:渡された数値(配列)からヤスオくんはモルガナ先輩と付き合えるか判断する
第1引数:各コインの枚数とお賽銭額の配列
戻り値:付き合える場合:OK, 付き合えない場合:NO
*/
char judge_dating(unsigned int* money){
  char ret = 0;
  unsigned int temp_money = 0;
  int i = 0;

  temp_money = money[6];

  while(i < (NUM_INPUT - 1)){
    if((temp_money >= coin_value[i]) && (money[i] > 0)){
      temp_money -= coin_value[i];
      money[i] -= 1;
    } else {
      i++;
    }
  }

  if(temp_money == 0){
    ret = OK;
  } else {
    ret = NO;
  }

  return ret;
}
0