結果

問題 No.45 回転寿司
ユーザー SagTokiSagToki
提出日時 2018-05-22 13:45:35
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,063 bytes
コンパイル時間 3,923 ms
コンパイル使用メモリ 76,252 KB
実行使用メモリ 58,148 KB
最終ジャッジ日時 2023-09-11 01:12:17
合計ジャッジ時間 10,199 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 180 ms
55,888 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 132 ms
55,748 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 144 ms
56,384 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 139 ms
55,512 KB
testcase_12 AC 167 ms
56,100 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 156 ms
55,872 KB
testcase_17 AC 170 ms
55,712 KB
testcase_18 WA -
testcase_19 AC 172 ms
55,528 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 121 ms
55,664 KB
testcase_23 AC 122 ms
55,540 KB
testcase_24 AC 122 ms
55,548 KB
testcase_25 AC 122 ms
55,152 KB
testcase_26 AC 168 ms
55,940 KB
testcase_27 AC 177 ms
55,988 KB
testcase_28 WA -
testcase_29 AC 166 ms
55,800 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 122 ms
55,728 KB
testcase_33 AC 181 ms
56,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.InputMismatchException;

public class Sushi {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        try{
            //寿司の数を入力して数値が条件内かチェック
            int N = scanner.nextInt();
            if(N < 1 || N > 1000){
                System.out.println("Nは1以上1000以下で入力してください");
                System.exit(0);
            }
            
            //食べた寿司の美味しさを数える長さNのカウント配列を定義
            int[] Count = new int[N];
            
            //与えられた個数の美味しさ数をint配列に順番に入れていく
            int[] Delicious = new int[N];
            for(int i = 0 ; i < N ; i++){
                Delicious[i] = scanner.nextInt();
                if(Delicious[i] < 1 || Delicious[i] > 100){
                    System.out.println("美味しさは1以上100以下で入力してください");
                    System.exit(0);
                }
            }
            
            //もしもNが1だった場合その値が答えになるので先に条件分岐
            if(N > 1){
                Count[1] = Math.max(Delicious[0],Delicious[1]);
            }else{
                System.out.println(Delicious[0]);
                System.exit(0);
            }
            
            //Nが2以上の場合に一番美味しさ数が多くなる組み合わせを動的計画法で抽出
            for(int j = 2 ; j < N ; j++){
                Count[j] = Math.max(Count[j - 1],Count[j-2] + Delicious[j]);
            }
            
            //Count[N - 1]が取りうる最大の美味しさ数の合計になっているのでプリントする
            System.out.println(Count[N - 1]);

        }catch(InputMismatchException e){
            System.out.println("数字で入力してください");
        }catch(Exception E){
            System.out.println("予期せぬエラーです");
        }
    }
}
0