結果

問題 No.10 +か×か
ユーザー takeya_okinotakeya_okino
提出日時 2017-08-12 20:28:47
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,951 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 79,992 KB
実行使用メモリ 130,292 KB
最終ジャッジ日時 2024-04-21 07:17:24
合計ジャッジ時間 5,951 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
54,308 KB
testcase_01 AC 110 ms
53,116 KB
testcase_02 AC 108 ms
52,952 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 124 ms
41,344 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 121 ms
40,212 KB
testcase_11 AC 132 ms
41,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int t = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i < n; i++) {
      a[i] = sc.nextInt();
    }
    int[][] dp = new int[n + 1][t + 1];
    String[][] exp = new String[n + 1][t + 1];
    dp[1][a[0]] = 1;
    exp[1][a[0]] = "";
    if(a[0] + a[1] < t + 1) {
      dp[2][a[0] + a[1]] = 1;
      exp[2][a[0] + a[1]] = "1";
    }
    if(a[0] * a[1] < t + 1) {
      dp[2][a[0] * a[1]] = 1;
      if(a[0] * a[1] != a[0] + a[1]) {
        exp[2][a[0] * a[1]] = "2"; 
      }
    }
    for(int i = 3; i < n + 1; i++) {
      for(int j = 1; j < t + 1; j++) {
        int x = 0;
        int y = 0;
        if(j > a[i - 1]) {
          if(dp[i - 1][j - a[i - 1]] == 1) {
            dp[i][j] = 1;
            x = Integer.parseInt(exp[i - 1][j - a[i - 1]]);
          }
          if(dp[i - 1][j / a[i - 1]] == 1) {
            dp[i][j] = 1;
            y = Integer.parseInt(exp[i - 1][j / a[i - 1]]);
          }
          if(x > 0 && y > 0) {
            if(x <= y) {
              exp[i][j] = exp[i - 1][j - a[i - 1]] + "1";
            } else {
              exp[i][j] = exp[i - 1][j / a[i - 1]] + "2";
            }
          }
          if(x > 0 && y == 0) {
            exp[i][j] = exp[i - 1][j - a[i - 1]] + "1";
          }
          if(x == 0 && y > 0) {
            exp[i][j] = exp[i - 1][j / a[i - 1]] + "2";
          }
        } else {
          if(j % a[i - 1] == 0) {
            if(dp[i - 1][j / a[i - 1]] == 1) {
              dp[i][j] = 1;
              exp[i][j] = exp[i - 1][j / a[i - 1]] + "2";
            }
          }
        }
      }
    }
    String e = exp[n][t];
    String ans = "";
    for(int i = 0; i < e.length(); i++) {
      if(e.charAt(i) == '1') {
        ans += "+";
      } else {
        ans += "*";
      }
    }
    System.out.println(ans);
  }
}
0