結果

問題 No.193 筒の数式
ユーザー YamaKasaYamaKasa
提出日時 2018-07-31 22:55:56
言語 Java
(openjdk 23)
結果
AC  
実行時間 117 ms / 1,000 ms
コード長 2,105 bytes
コンパイル時間 2,553 ms
コンパイル使用メモリ 87,912 KB
実行使用メモリ 54,144 KB
最終ジャッジ日時 2024-09-19 16:39:10
合計ジャッジ時間 5,042 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String S = scan.next();
		scan.close();
		int l = S.length();
		String ary[] = S.split("");
		int max = Integer.MIN_VALUE;
		for(int i = 0; i < l; i++) {
			String s = "";
			// 前半部分
			for(int j = i; j < l; j++) {
				s += ary[j];
			}
			// 後半部分
			for(int j = 0; j < i; j++) {
				s += ary[j];
			}
			if(!s.substring(0, 1).matches("[0-9]")) {
				continue;
			}
			if(!s.substring(l - 1, l).matches("[0-9]")) {
				continue;
			}
			Deque<String> queue0 = new ArrayDeque<String>();
			Deque<Integer> queue = new ArrayDeque<Integer>();
			String num = "";
			int t0 = 0;
			int index = 0;
			for(int j = 0; j < l; j++) {
				String t1 = s.substring(j, j + 1);
				if(t1.matches("[0-9]")) {
					num += t1;
				}else {
					queue0.add(t1);
					int num1 = zeroToNum(num);
					num = "";
					queue.add(num1);
					index = j;
				}
			}
			int last = zeroToNum(s.substring(index + 1, l));
			queue.add(last);
//			for(int j : queue) {
//				System.out.print(j + " ");
//			}
//			System.out.println();
//			for(String o : queue0) {
//				System.out.print(o + " ");
//			}
//			System.out.println();
//			//System.out.println(index);
//			System.out.println(s);
			//System.out.println(queue0.peek());
			for(String ope : queue0) {
				if(ope.equals("-")) {
					int num1 = queue.pop();
					int num2 = queue.pop();

					t0 = num1 - num2;

					queue.push(t0);
				}else {
					int num1 = queue.pop();
					int num2 = queue.pop();
					t0 = num1 + num2;
					queue.push(t0);
				}
			}
			t0 = queue.pop();
			if(max < t0) {
				max = t0;
			}
		}
		System.out.println(max);
	}
	static int zeroToNum(String s) {
		int l = s.length();
		if(l == 1 || !s.substring(0, 1).equals("0")) {
			return Integer.parseInt(s);
		}else {
			for(int i = 1; i < l; i++) {
				char c = s.charAt(i);
				if(c != 0) {
					String num = s.substring(i, l);
					return Integer.parseInt(num);
				}
			}
			return 0;
		}
	}
}
0