結果

問題 No.49 算数の宿題
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2015-06-24 17:20:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 155 ms / 5,000 ms
コード長 1,225 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 76,096 KB
実行使用メモリ 56,904 KB
最終ジャッジ日時 2023-08-24 17:26:28
合計ジャッジ時間 4,358 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 153 ms
55,036 KB
testcase_01 AC 153 ms
54,968 KB
testcase_02 AC 153 ms
56,904 KB
testcase_03 AC 155 ms
56,608 KB
testcase_04 AC 154 ms
56,708 KB
testcase_05 AC 154 ms
56,768 KB
testcase_06 AC 152 ms
56,864 KB
testcase_07 AC 153 ms
56,544 KB
testcase_08 AC 152 ms
56,792 KB
testcase_09 AC 152 ms
56,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashSet;
import java.util.Scanner;

public class Main {
	public static int solve(String S){
		int sum = 0, i = 0;
		
		String ss = "";
		String before = "";
		
		while(true){
			char si = S.charAt(i);
			if (si == '*' || si == '+') {
				sum = Integer.parseInt(ss);
				before = String.valueOf(si);
				i++;
				break;
			}
			else{
				ss += si;
				i++;
			}
		}
		
		ss = "";
		
		while(i < S.length()){
			char si = S.charAt(i);
			if (si == '*') {
				if(before.equals("*")){
					sum += Integer.parseInt(ss);
				}
				else {
					sum *= Integer.parseInt(ss);
				}
				before = String.valueOf(si);
				ss = "";
			}
			else if (si == '+') {
				if (before.equals("*")) {
					sum += Integer.parseInt(ss);
				}
				else{
					sum *= Integer.parseInt(ss);
				}
				before = String.valueOf(si);
				ss = "";
			}
			else{
				ss += String.valueOf(si);
				if (i == S.length() - 1) {
					if (before.equals("*")) {
						sum += Integer.parseInt(ss);
					}
					else {
						sum *= Integer.parseInt(ss);
					}					
				}
			}
			i++;
		}
		return sum;
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		String S = sc.next();

		System.out.println(solve(S));
	}
}
0