結果

問題 No.49 算数の宿題
ユーザー kuramukuramu
提出日時 2016-02-11 10:43:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 1,462 bytes
コンパイル時間 1,837 ms
コンパイル使用メモリ 74,480 KB
実行使用メモリ 49,652 KB
最終ジャッジ日時 2023-08-24 17:29:08
合計ジャッジ時間 2,909 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,400 KB
testcase_01 AC 41 ms
49,516 KB
testcase_02 AC 41 ms
49,196 KB
testcase_03 AC 41 ms
49,476 KB
testcase_04 AC 41 ms
49,136 KB
testcase_05 AC 41 ms
49,288 KB
testcase_06 AC 40 ms
49,340 KB
testcase_07 AC 41 ms
49,432 KB
testcase_08 AC 40 ms
49,312 KB
testcase_09 AC 41 ms
49,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	 public static void main(String[] args) {
	      BufferedReader stdReader =new BufferedReader(new InputStreamReader(System.in));
	      try {
	    	  String line = stdReader.readLine();
	    	  int ans = 0;
	    	  int index = 0;
	    	  String op = "";
	    	  for(int i=0;i<line.length();i++){
	    		  if(line.charAt(i) == '+'){
	    			  if(op.equals("+")){
	    				  ans += Integer.parseInt(line.substring(index, i));
	    			  }else if(op.equals("*")){
	    				  ans *= Integer.parseInt(line.substring(index, i));
	    			  }else{
	    				  ans = Integer.parseInt(line.substring(index, i));
	    			  }
	    			  op = "*";
	    			  index = i+1;
	    		  }else if(line.charAt(i) == '*'){
	    			  if(op.equals("+")){
	    				  ans += Integer.parseInt(line.substring(index, i));
	    			  }else if(op.equals("*")){
	    				  ans *= Integer.parseInt(line.substring(index, i));
	    			  }else{
	    				  ans = Integer.parseInt(line.substring(index, i));
	    			  }
	    			  op = "+";
	    			  index = i+1;
	    		  } 
	    	  }
			  if(op.equals("+")){
				  ans += Integer.parseInt(line.substring(index, line.length()));
			  }else if(op.equals("*")){
				  ans *= Integer.parseInt(line.substring(index, line.length()));
			  }
	    	  System.out.println(ans);
	      } catch (IOException e) {
			e.printStackTrace();
	      }
	}
}
0