結果

問題 No.894 二種類のバス
ユーザー GinFizz1185GinFizz1185
提出日時 2019-11-19 20:42:55
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 3,134 bytes
コンパイル時間 716 ms
コンパイル使用メモリ 31,744 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 00:36:59
合計ジャッジ時間 3,260 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 RE -
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define STR_LENGTH 23
void str_cut(char*, unsigned long long*);
unsigned long long calGCD(unsigned long long arg1, unsigned long long arg2);
unsigned long long calDep(unsigned long long dayLong, unsigned long long interval);

int main(void)
{
  char str[STR_LENGTH];
  unsigned long long input[3] = {0};
  unsigned long long gcd = 0;
  unsigned long long lcm = 0;
  unsigned long long depNumA = 0;
  unsigned long long depNumB = 0;
  unsigned long long depNumAB = 0;

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

  gcd = calGCD(input[1], input[2]);
  // printf("GCD = %d\n", gcd);
  lcm = input[1] * input[2] / gcd;

  depNumA = calDep(input[0], input[1]);
  depNumB = calDep(input[0], input[2]);
  depNumAB = calDep(input[0], lcm);
  // printf("depA = %d\n depB = %d\n depAB = %d\n", depNumA, depNumB, depNumAB);

  printf("%d\n", 1 + depNumA + depNumB - depNumAB);

  return 0;
}

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

  while('\0' != str[i]){
    if(' ' == str[i]){
      str[i] = '\0';
      arg2[num] = (unsigned long long) strtoull(&str[start_str], NULL, 10);
      ++num;
      ++i;
      start_str = i;
    }
    ++i;
  }

  arg2[num] = (unsigned long long) strtoull(&str[start_str], NULL, 10);

  return;
}

/*
関数概要:ユークリッドの互除法により最大公約数を求める
第1引数[unsigned long long]:最大公約数を求めたい値1
第2引数[unsigned long long]:最大公約数を求めたい値2
戻り値:[unsigned long long]:第1引数と第2引数の最大公約数
備考:第1引数と第2引数の大小は問わない
*/
unsigned long long calGCD(unsigned long long arg1, unsigned long long arg2)
{
  unsigned long long largerNum = 0;
  unsigned long long smallerNum = 0;
  unsigned long long remainder = 0;

  // 大小比較
  if(arg1 >= arg2)
  {
    largerNum = arg1;
    smallerNum = arg2;
  }
  else
  {
    largerNum = arg2;
    smallerNum = arg1;
  }
  
  if(smallerNum != 0){
    while(1){
      remainder = largerNum % smallerNum;
      // printf("remainder = %d\n", remainder);
      if(0 != remainder)
      {
        largerNum = smallerNum;
        smallerNum = remainder;
      } else
      {
        break;
      }
    }
  }

  return smallerNum;
  
}

/*
関数概要:一日の長さと出発の間隔より、一日の出発回数を求める
第1引数[unsigned long long]:一日の長さ
第2引数[unsigned long long]:出発間隔
戻り値:[unsigned long long]:出発回数
備考:一日のちょうど終わりに出発したものはカウントしない
*/
unsigned long long calDep(unsigned long long dayLong, unsigned long long interval)
{
  unsigned long long depNum = 0;
  
  if(0 != interval)
  {
    depNum = dayLong / interval;
    if(0 == (dayLong % interval))
    {
      depNum -= 1;
    }
  }

  return depNum;
}
0