結果

問題 No.45 回転寿司
ユーザー SagTokiSagToki
提出日時 2018-05-22 14:26:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 183 ms / 5,000 ms
コード長 2,163 bytes
コンパイル時間 4,388 ms
コンパイル使用メモリ 80,204 KB
実行使用メモリ 56,480 KB
最終ジャッジ日時 2023-08-27 16:27:48
合計ジャッジ時間 10,378 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 166 ms
56,184 KB
testcase_01 AC 181 ms
55,720 KB
testcase_02 AC 168 ms
55,888 KB
testcase_03 AC 175 ms
56,272 KB
testcase_04 AC 164 ms
55,952 KB
testcase_05 AC 129 ms
55,508 KB
testcase_06 AC 167 ms
56,480 KB
testcase_07 AC 174 ms
56,180 KB
testcase_08 AC 151 ms
56,148 KB
testcase_09 AC 178 ms
56,120 KB
testcase_10 AC 167 ms
55,992 KB
testcase_11 AC 135 ms
55,640 KB
testcase_12 AC 173 ms
55,976 KB
testcase_13 AC 132 ms
56,044 KB
testcase_14 AC 125 ms
56,088 KB
testcase_15 AC 159 ms
56,244 KB
testcase_16 AC 155 ms
55,952 KB
testcase_17 AC 169 ms
56,312 KB
testcase_18 AC 153 ms
55,672 KB
testcase_19 AC 162 ms
56,072 KB
testcase_20 AC 128 ms
55,792 KB
testcase_21 AC 128 ms
56,124 KB
testcase_22 AC 118 ms
55,744 KB
testcase_23 AC 119 ms
55,868 KB
testcase_24 AC 118 ms
55,732 KB
testcase_25 AC 118 ms
55,580 KB
testcase_26 AC 162 ms
56,312 KB
testcase_27 AC 175 ms
56,348 KB
testcase_28 AC 160 ms
55,688 KB
testcase_29 AC 166 ms
55,720 KB
testcase_30 AC 175 ms
55,940 KB
testcase_31 AC 118 ms
55,852 KB
testcase_32 AC 119 ms
56,024 KB
testcase_33 AC 183 ms
56,348 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++){
                if(Count[0] == 0){
                    Count[0] = Delicious[0];
                }
                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