結果

問題 No.193 筒の数式
ユーザー kenji_shioyakenji_shioya
提出日時 2016-06-22 18:09:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 124 ms / 1,000 ms
コード長 1,419 bytes
コンパイル時間 3,395 ms
コンパイル使用メモリ 80,180 KB
実行使用メモリ 54,392 KB
最終ジャッジ日時 2024-04-20 00:21:32
合計ジャッジ時間 6,385 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
52,756 KB
testcase_01 AC 121 ms
54,060 KB
testcase_02 AC 116 ms
53,892 KB
testcase_03 AC 105 ms
53,264 KB
testcase_04 AC 99 ms
53,024 KB
testcase_05 AC 103 ms
53,048 KB
testcase_06 AC 116 ms
54,228 KB
testcase_07 AC 104 ms
53,008 KB
testcase_08 AC 114 ms
54,092 KB
testcase_09 AC 99 ms
54,312 KB
testcase_10 AC 124 ms
54,144 KB
testcase_11 AC 106 ms
53,024 KB
testcase_12 AC 117 ms
54,144 KB
testcase_13 AC 112 ms
53,268 KB
testcase_14 AC 118 ms
53,948 KB
testcase_15 AC 117 ms
54,284 KB
testcase_16 AC 101 ms
52,792 KB
testcase_17 AC 117 ms
54,116 KB
testcase_18 AC 116 ms
54,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Exercise111{
  public static void main (String[] args){

    Scanner sc = new Scanner(System.in);

    String s = sc.next();

    boolean prevIsNum = false;
    int max = calStr(s);
    for(int i = 0; i < s.length(); i++){
      if(prevIsNum && Character.isDigit(s.charAt(i))){
        String f = s.substring(0, i);
        String l = s.substring(i);
        String newStr = l + f;
        int num = calStr(newStr);
        max = Math.max(max, num);
      }else{
        if(Character.isDigit(s.charAt(i))){
          prevIsNum = true;
        }else{
          prevIsNum = false;
        }
      }
    }
    System.out.println(max);
  }
  private static int calStr(String s){
    char[] a = s.toCharArray();
    int count = 0;
    for(int i = 0; i < a.length; i++){
      if(a[i] == '+' || a[i] == '-'){
        count++;
      }
    }
    String[] num = new String[count + 1];
    Arrays.fill(num, "");
    char[] op = new char[count];
    int index = 0;
    for(int i = 0; i < a.length; i++){
      if(Character.isDigit(a[i])){
        num[index] += Character.toString(a[i]);
      }else{
        op[index] = a[i];
        index++;
      }
    }
    int sum = Integer.parseInt(num[0]);
    for(int i = 1; i < num.length; i++){
      if(op[i - 1] == '+'){
        sum += Integer.parseInt(num[i]);
      }else{
        sum -= Integer.parseInt(num[i]);
      }
    }
    return sum;
  }
}
0