結果

問題 No.504 ゲーム大会(ランキング)
ユーザー nihi9119nihi9119
提出日時 2017-05-30 18:05:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 685 ms / 2,000 ms
コード長 3,256 bytes
コンパイル時間 3,493 ms
コンパイル使用メモリ 77,884 KB
実行使用メモリ 69,484 KB
最終ジャッジ日時 2023-10-21 18:04:40
合計ジャッジ時間 10,619 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
52,556 KB
testcase_01 AC 54 ms
53,564 KB
testcase_02 AC 55 ms
53,596 KB
testcase_03 AC 53 ms
53,600 KB
testcase_04 AC 52 ms
53,592 KB
testcase_05 AC 53 ms
53,592 KB
testcase_06 AC 52 ms
53,552 KB
testcase_07 AC 684 ms
69,404 KB
testcase_08 AC 683 ms
69,484 KB
testcase_09 AC 685 ms
69,268 KB
testcase_10 AC 665 ms
69,324 KB
testcase_11 AC 681 ms
68,384 KB
testcase_12 AC 660 ms
66,920 KB
testcase_13 AC 661 ms
68,872 KB
testcase_14 AC 662 ms
69,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package test_5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * No.504 ゲーム大会(ランキング)
 *
 * K 君はゲーム大会にやってきた。 このゲームは、できるだけ高いスコアを目指すゲームである。 K君を含めてNN人が参加している。
 * 順番に1回ずつプレイしていき、随時ランキングが更新されていく。 K君は最初にプレイする。
 * それぞれの人がプレイし終わった時に、K君が何位にいるかを求めよ。 スコアが高い方の順位が上になる。 同点の場合は先にプレイした方の順位が上とする。
 */

public class Question_06_0526_1 {

	static final int NUM_MIN = 1;
	static final int NUM_MAX = (int) Math.pow(10, 5);
	static final int SCORE_MIN = 1;
	static final int SCORE_MAX = (int) Math.pow(10, 9);

	static int[] kRankList; // k君の順位
	static int kScore; // k君のスコア

	public static void main(String[] args) {
		InputStreamReader re = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(re);

		try {
			// N(人数)取得
			String stringPeople = br.readLine();
			int numPeople = Integer.parseInt(stringPeople);
			boolean errorFlg = false;

			// 人数が有効範囲か確認
			if (NumJudg(numPeople, NUM_MIN, NUM_MAX)) {
				// 人数で初期化
				kRankList = new int[numPeople];
				// 人数分処理する
				for (int i = 0; i < numPeople; i++) {
					int score = Integer.parseInt(br.readLine());

					// スコアが有効範囲か確認
					if (NumJudg(score, SCORE_MIN, SCORE_MAX)) {
						if (i == 0) {
							kScore = score;
							kRankList[i] = 1; // 最初はK君が1位
						} else {
							kRankList[i] = RankJudg(score, i);
						}
					} else {
						errorFlg = true;
						System.out.println("スコアの数値が有効範囲外です。");
						break;
					}
				}
				// 順位表示
				if (!errorFlg) {
					for (int ranking : kRankList) {
						System.out.println(ranking);
					}
				}
			} else {
				System.out.println("人数の数値が有効範囲外です。");
			}
		} catch (NumberFormatException e) {
			System.out.println("数字を入力して下さい。");
		} catch (IOException e) {
			System.out.println("エラーが発生しました。");
		} finally {
			try {
				re.close();
				br.close();
			} catch (IOException e) {
				System.out.println("InputStreamReader、BufferedReaderクローズ中にエラーが発生しました");
			}

		}
	}

	/**
	 * 有効値判定
	 * @param num 判定する数字
	 * @param max 最大値
	 * @param min 最小値
	 * @return 範囲内ならtrue,範囲外ならfalseを返す
	 */
	private static boolean NumJudg(int num, int min, int max) {
		Boolean result = false;
		if (min <= num && num <= max) {
			result = true;
		}
		return result;
	}

	/**
	 * 順位判定
	 * @param score 点数
	 * @param count 何人目か
	 * @return 現在の順位
	 */
	static private int RankJudg(int score, int count) {
		//抜かされたらランキングを更新
		if (score > kScore) {
			kRankList[count] = kRankList[count - 1] + 1;
		} else {
			kRankList[count] = kRankList[count - 1];
		}
		return kRankList[count];
	}

}
0