結果
問題 | No.193 筒の数式 |
ユーザー |
![]() |
提出日時 | 2015-04-27 11:47:36 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 62 ms / 1,000 ms |
コード長 | 2,455 bytes |
コンパイル時間 | 4,547 ms |
コンパイル使用メモリ | 82,648 KB |
実行使用メモリ | 37,568 KB |
最終ジャッジ日時 | 2024-07-05 04:50:30 |
合計ジャッジ時間 | 5,590 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 16 |
ソースコード
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; public class No193 { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); int max = Integer.MIN_VALUE; String S = br.readLine(); int Slength = S.length(); String head = ""; String tail = ""; for (int i = 0; i < Slength; i++) { head = S.substring(0, 1); tail = S.substring(Slength - 1, Slength); // 先頭としっぽが記号だったらcontinue if (head.equals("+") || head.equals("-") || tail.equals("+") || tail.equals("-")) { S = rotateString(S, 1); continue; } max = Math.max(calc(S), max); // 円筒のきる部分を変える S = rotateString(S, 1); } System.out.println(max); } catch (Exception e) { System.err.println("Error:" + e.getMessage()); } } private static int calc(String S) { Queue<String> operatorQueue = new LinkedList<String>(); Queue<Integer> numQuque = new LinkedList<Integer>(); String[] numArray = S.split("[\\+-]"); // 数字をキューに格納 for (int i = 0; i < numArray.length; i++) { numQuque.add(Integer.parseInt(numArray[i])); } int sLnegth = S.length(); // 演算子をキューに格納 for (int i = 0; i < sLnegth; i++) { if (!S.substring(i, i + 1).matches("[0-9]")) operatorQueue.add(S.substring(i, i + 1)); } // 計算 int ans = numQuque.poll(); int num = 0; String ope = ""; while (!numQuque.isEmpty()) { num = numQuque.poll(); ope = operatorQueue.poll(); if (ope.equals("+")) { ans = ans + num; } else { ans = ans - num; } } return ans; } static String rotateString(String S, int n) { StringBuilder sb = new StringBuilder(S); String head = sb.substring(0, n); sb.delete(0, n); sb.append(head); return sb.toString(); } }