解题思路:
注意事项:
参考代码:
import java.util.Scanner;
public class qisan {
public static void main(String[] args) {
int count = 0;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str = scanner.next();
int opIndex = str.indexOf("+");// 返回出现操作符+ -的字符串位置-1表示没有找到
if (opIndex == -1) {
opIndex = str.indexOf("-");
}
int eqPos = str.indexOf("=");
String aStr = str.substring(0, opIndex);// 取a的字符串
String bStr = str.substring(opIndex + 1, eqPos);// 取b的字符串
String resultStr = str.substring(eqPos + 1);// 结果的字符串
int a = Integer.parseInt(aStr);
int b = Integer.parseInt(bStr);
int c;
if (!resultStr.endsWith("?")) {
c = Integer.parseInt(resultStr);
} else {
c = -32768;
}
char op = str.charAt(opIndex);
if (op == '+' && c == a + b) {
count++;
} else if (op == '-' && c == a - b) {
count++;
}
}
System.out.println(count);
}
}
( 参考了下,湖南省第六届 中信软件教育杯 大学生程序设计大赛试题 第二题 )
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:535 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:2092 |
妹子杀手的故事 (C语言代码)浏览:1054 |
C语言训练-大、小写问题 (C语言代码)浏览:681 |
C语言程序设计教程(第三版)课后习题8.5 (C语言代码)浏览:559 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:613 |
明明的随机数 (C语言代码)浏览:953 |
简单的a+b (C语言代码)浏览:558 |
C语言程序设计教程(第三版)课后习题11.1 (C++代码)浏览:689 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:473 |