题目
示例1
输入
bb1234aa
输出
10
示例2
输入
bb12-34aa
输出
-31
示例3
输入
bb0012-0034aa
输出
-31
题解
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long res = 0;
String s = scanner.nextLine();
int n = s.length();
for (int i = 0; i < n; i++) {
if (!Character.isDigit(s.charAt(i)) && s.charAt(i) != '-') {
continue;
}
if (s.charAt(i) == '-') {
int j = i + 1;
long num = 0;
while (j < n && Character.isDigit(s.charAt(j))) {
num = num * 10 + s.charAt(j) - '0';
j++;
}
res -= num;
i = j - 1;
} else {
res += s.charAt(i) - '0';
}
}
System.out.println(res);
}
}