結果

問題 No.193 筒の数式
ユーザー ぴろずぴろず
提出日時 2015-04-26 22:12:35
言語 Java19
(openjdk 21)
結果
AC  
実行時間 118 ms / 1,000 ms
コード長 1,610 bytes
コンパイル時間 2,440 ms
コンパイル使用メモリ 79,440 KB
実行使用メモリ 56,156 KB
最終ジャッジ日時 2023-09-18 11:56:34
合計ジャッジ時間 5,152 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
55,736 KB
testcase_01 AC 114 ms
55,724 KB
testcase_02 AC 117 ms
55,808 KB
testcase_03 AC 116 ms
55,804 KB
testcase_04 AC 117 ms
55,900 KB
testcase_05 AC 115 ms
55,716 KB
testcase_06 AC 115 ms
55,860 KB
testcase_07 AC 116 ms
55,844 KB
testcase_08 AC 115 ms
55,988 KB
testcase_09 AC 114 ms
55,708 KB
testcase_10 AC 115 ms
55,688 KB
testcase_11 AC 115 ms
55,852 KB
testcase_12 AC 118 ms
55,824 KB
testcase_13 AC 115 ms
55,696 KB
testcase_14 AC 114 ms
55,764 KB
testcase_15 AC 117 ms
55,872 KB
testcase_16 AC 116 ms
55,460 KB
testcase_17 AC 116 ms
56,156 KB
testcase_18 AC 116 ms
55,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no193;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		char[] s = sc.next().toCharArray();
		int n = s.length;
		long ans = - (1<<60);
		for(int i=0;i<n;i++) {
			char[] t = new char[n];
			for(int j=0;j<n;j++) {
				t[j] = s[(i+j)%n];
			}
			if (Character.isDigit(t[0]) && Character.isDigit(t[n-1])) {
				ans = Math.max(ans, new Expression(String.valueOf(t)).exp());
			}
		}
		System.out.println(ans);
	}
}
class Expression {
	private String expression;
	private int begin = 0;
	public Expression(String expression) {
		this.expression = expression;
	}
	public long exp() {
		long num = term();
		while(begin<expression.length()) {
			char c = expression.charAt(begin);
			if (c=='+') {
				begin++;
				num+=term();
			}else if(c=='-') {
				begin++;
				num-=term();
			}else{
				break;
			}
		}
		return num;
	}
	private long term() {
		long num = factor();
		while(begin<expression.length()) {
			char c = expression.charAt(begin);
			if (c=='*') {
				begin++;
				num*=factor();
			}else if(c=='/') {
				begin++;
				num/=factor();
			}else{
				break;
			}
		}
		return num;
	}
	private long factor() {
		char c = expression.charAt(begin);
		if (c=='(') {
			begin++; //(
			long num = exp();
			begin++; //)
			return num;
		}else{
			return number();
		}
	}
	private long number() {
		long num = 0;
		while(begin<expression.length()) {
			if (Character.isDigit(expression.charAt(begin))) {
				num*=10;
				num+=expression.charAt(begin)-'0';
				begin++;
			}else{
				break;
			}
		}
		return num;
	}
}
0