結果
| 問題 |
No.112 ややこしい鶴亀算
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-06-08 19:52:41 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 98 ms / 5,000 ms |
| コード長 | 3,012 bytes |
| コンパイル時間 | 3,478 ms |
| コンパイル使用メモリ | 83,836 KB |
| 実行使用メモリ | 52,984 KB |
| 最終ジャッジ日時 | 2024-09-22 13:35:03 |
| 合計ジャッジ時間 | 6,568 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 23 |
ソースコード
package test_5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* No.112 ややこしい鶴亀算 http://yukicoder.me/problems/no/112
* N 匹の動物がいる。
* 動物たちは 1 から N まで番号が振られている。
* また、動物たちはそれぞれ鶴か亀のいずれかである。
* ご存知の通り、鶴の足は 2 本、亀の足は 4 本である。
*/
public class Question_15_0608_2 {
static final int MIN = 2;
static final int MAX = 50;
static final String TURU = "crane";
static final String KAME = "turtle";
public static void main(String[] args) {
InputStreamReader re = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(re);
int count1 = 0; //1種類目が数えた数
String firstAnimal = ""; //最初の動物
try {
int animalCount = Integer.parseInt(br.readLine());
String[] legStrList = br.readLine().split(" ");
count1 = Integer.parseInt(legStrList[0]);
//有効値判定
if (NumJudg(animalCount, MIN, MAX)) {
int legSum = 0; //合計
int count2 = 0; //2種類目が数えた数
int firstAnimalCount = 0; //最初の動物の数
int SecondAnimalCoun = 0; //次の動物の数
//数える 2種類の数を探す(なければどっちも同じ)
for (int i = 0; i < legStrList.length; i++) {
int num = Integer.parseInt(legStrList[i]);
if (num == count1) {
firstAnimalCount++;
}
else {
if (count2 == 0) {
count2 = num;
}
}
}
//全部同じ動物の場合
if (count2 == 0) {
SecondAnimalCoun = 0;
if (count1 / animalCount == 1) {
firstAnimal = TURU;
} else {
firstAnimal = KAME;
}
} else {
//数の少ないほうが亀
SecondAnimalCoun = animalCount - firstAnimalCount;
if (count1 < count2) {
firstAnimal = KAME;
} else {
firstAnimal = TURU;
}
}
//鶴、亀の順に出力
if (firstAnimal.equals(TURU)) {
System.out.println(firstAnimalCount + " " + SecondAnimalCoun);
} else {
System.out.println(SecondAnimalCoun + " " + firstAnimalCount);
}
} 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 input 判定するもの
* @param max 最大値
* @param min 最小値
* @return 範囲内ならtrue,範囲外ならfalseを返す
*/
private static boolean NumJudg(int input, int min, int max) {
Boolean result = false;
if (min <= input && input <= max) {
result = true;
}
return result;
}
}