結果

問題 No.49 算数の宿題
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-11-15 00:51:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 119 ms / 5,000 ms
コード長 1,037 bytes
コンパイル時間 2,555 ms
コンパイル使用メモリ 77,420 KB
実行使用メモリ 56,136 KB
最終ジャッジ日時 2023-08-24 17:34:30
合計ジャッジ時間 3,842 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
55,744 KB
testcase_01 AC 117 ms
53,800 KB
testcase_02 AC 116 ms
55,440 KB
testcase_03 AC 117 ms
55,772 KB
testcase_04 AC 119 ms
55,756 KB
testcase_05 AC 119 ms
55,616 KB
testcase_06 AC 117 ms
55,908 KB
testcase_07 AC 115 ms
55,700 KB
testcase_08 AC 117 ms
55,676 KB
testcase_09 AC 116 ms
56,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String S = sc.next();
        String [] ss = S.split("\\*|\\+");
        int [] n = new int [ss.length];
        
        for(int i=0; i<ss.length; i++){
            n[i] = Integer.parseInt(ss[i]);
        }
        int r = n[0];
        int i = 1;
        int a = ind(S,"*");
        int b = ind(S,"+");
        int min = Math.min(a,b);
        while(true){
            String t =S.substring(min,min+1);
            if( t.equals("*") ){
                r=r+n[i];
            }else{
                r=r*n[i];
            }
            i++;
            S = S.substring(min+1);
            a = ind(S,"*");
            b = ind(S,"+");
            if(a==b){break;}
            min = Math.min(a,b);
        }
        System.out.println(r);
    }
    static int ind(String a, String b){
        int x = a.indexOf(b);
        if(x==-1){
            x=Integer.MAX_VALUE;
        }
        return x;
    }
}
0