Java弟弟的作业
**大學生程序設計競賽(非專業組) **
標題Problem A: 弟弟的作業
Time Limit: 1 Sec Memory Limit: 128 MB
Description
Jock的弟弟剛做完了“100以內數的加法”這部分的作業,請你幫他檢查一下。每道題目(包括弟弟的答案)的格式為a + b = c,其中a和b是作業中給出的,均為不超過100的非負整數;c是弟弟算出的答案,是不超過200的非負整數。
Input
輸入數據包含多組,每組數據占一行,每行包含一道題目,格式保證符合上述規定,且不包含任何空白字符。輸入的所有整數均不含前導0。
Output
對于每個輸入算式,如果算式正確輸出“Yes”,否則輸出“No”。
Sample Input Copy
1+2=3
3-1=5
Sample Output Copy
Yes
No
Java代代碼:
import java.util.*; public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {String str = sc.nextLine();int x = str.indexOf('+');int y = str.indexOf('-');int z = Integer.parseInt(str.substring(str.indexOf('=') + 1)); if (x != -1) {if (Integer.valueOf(str.substring(0, x))+ Integer.valueOf(str.substring(x + 1,str.lastIndexOf('='))) == z)System.out.println("Yes");elseSystem.out.println("No");}else {if (Integer.valueOf(str.substring(0, y))- Integer.valueOf(str.substring(y + 1,str.lastIndexOf('='))) == z)System.out.println("Yes");elseSystem.out.println("No");}}} }運行結果:
Language: JavaResult: AcceptedTime:113 msMemory:10212 kb總結
- 上一篇: <学习笔记>从零开始自学Python-之
- 下一篇: linux修改blacklist.con