0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AtCoder ABC - 084- A&B&C

Posted at

AtCoder ABC 084 B&C

AtCoder - 084

A問題

  • $24hr+(24-M)hr$
    - 1日(12/31)+残り時間(12/30)
	private void solveA() {
		Scanner scanner = null;
		int numM = 0;

		try {
			scanner = new Scanner(System.in);
			numM = scanner.nextInt();

			System.out.println(24 + 24 - numM);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

B問題

  • 文字列Sは、$A + 1 + B$文字ある

    • 文字数が足りない場合はNG
  • 数字がA個 + '-' + 数字がB個という構造である必要がある

    • 上記に合致しているかを調べる
  • 実装は汚い

    • AとBを文字列として受け取って、数値にパースしてExceptionが出たらNGとしている
	private void solveB() {
		Scanner scanner = null;
		int numA = 0;
		int numB = 0;
		String s = "";

		try {
			scanner = new Scanner(System.in);
			numA = scanner.nextInt();
			numB = scanner.nextInt();
			s = scanner.next();
			try {
				String wkA = s.substring(0, numA);
				String wkB = s.substring(numA + 1, s.length());
				Integer.parseInt(wkA);
				Integer.parseInt(wkB);

				if (numA + numB + 1 == s.length()) {
					String wk = s.substring(numA, numA + 1);
					if (wk.equals("-")) {
						System.out.println("Yes");
						return;
					}
				}
			} catch (NumberFormatException e) {
				//				e.printStackTrace();
			}

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
		System.out.println("No");
	}

C問題

  • 再帰で解いた
  • 開通式時に$駅_i$にいた場合、駅Nまで到着できるのは最速で何秒後か?
  • 開始駅を駅_0から駅_(n-1)まで順次変更していき到着に必要な秒数を出力していく
    • 実装しながらコメント入れていったので説明はソースのコメント
	private void solveC() {
		Scanner scanner = null;

		try {
			scanner = new Scanner(System.in);
			int cNumN = scanner.nextInt();
			int[][] csf = new int[cNumN - 1][3];

			for (int i = 0; i < csf.length; i++) {
				csf[i][0] = scanner.nextInt();
				csf[i][1] = scanner.nextInt();
				csf[i][2] = scanner.nextInt();
			}

			for (int i = 0; i < csf.length; i++) {
				System.out.println(getTime(cNumN, csf, i, 0));
			}
			/**
			 * 最終駅は常に0
			 */
			System.out.println(0);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	private int getTime(int cNumN, int[][] csf, int currentI, int currentTime) {

		/**
		 * 最終駅まで付いたら完了
		 */
		if (currentI == cNumN - 1) {
			return currentTime;
		}

		int res = 0;

		int c = csf[currentI][0];
		int s = csf[currentI][1];
		int f = csf[currentI][2];

		/**
		 * 現在時刻がその駅のスタート時刻の前であった場合
		 * スタート時刻を採用
		 * 現在時刻がスタート時刻より後なら
		 * 現在時刻を採用
		 */
		if (currentTime < s) {
			res = s;
		} else {
			res = currentTime;
		}
		/**
		 * 現在時刻がfと同じタイミングなら、
		 * すぐに電車に乗れる。
		 *  =>ひとつ前のスタート時刻をそのまま利用できる
		 * 違うタイミングなら、
		 * 次の発車タイミングを待たなければいけない
		 */
		if (res % f != 0) {
			res = res + (f - res % f);
		}
		/**
		 * 発車したので移動時間を足す。
		 */
		res += c;
		/**
		 * 次の駅の処理を実行する
		 */
		res = getTime(cNumN, csf, currentI + 1, res);
		return res;
	}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?