提取字符串中的最长合法简单数学表达式,字符串长度最长的,并计算表达式的值。如果没有,则返回0
简单数学表达式只能包含以下内容
0-9数字,符号+-*
说明:
1.所有数字,计算结果都不超过long
2.如果有多个长度一样的,请返回第一个表达式的结果
3.数学表达式,必须是最长的,合法的
4.操作符不能连续出现,如+–+1是不合法的

输入描述:

字符串<

输出描述:

表达式值

示例1输入输出示例仅供调试,后台判题数据一般不包含示例

输入

1-2abcd

输出

-1

Java版本

import java.util.*;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String line = sc.nextLine();long res = 0;int maxLen = 0;int len = line.length();for (int i = 0; i < len; i++) {if (len - i <= maxLen) {break;}for (int j = i; j  maxLen) {maxLen = j + 1 - i;long first = Long.parseLong(matcher.group(1));String op = matcher.group(2);long second = Long.parseLong(matcher.group(3));switch (op) {case "+":res = first + second;break;case "-":res = first - second;break;case "*":res = first * second;break;}}}}System.out.println(res);}}

Python版本

import reline = input()res = 0max_len = 0length = len(line)for i in range(length):if length - i  max_len:max_len = j + 1 - ifirst = int(match.group(1))op = match.group(2)second = int(match.group(3))if op == '+':res = first + secondelif op == '-':res = first - secondelif op == '*':res = first * secondprint(res)

C++版本

#include #include #include using namespace std;int main() {string line;getline(cin, line);long long res = 0;int maxLen = 0;int len = line.length();for (int i = 0; i < len; i++) {if (len - i <= maxLen) {break;}for (int j = i; j  maxLen) {maxLen = j + 1 - i;long long first = stoll(match[1]);string op = match[2];long long second = stoll(match[3]);if (op == "+") {res = first + second;} else if (op == "-") {res = first - second;} else if (op == "*") {res = first * second;}}}}cout << res << endl;return 0;}

C语言版本

#include #include #include bool isOperator(char c) {return c == '+' || c == '-' || c == '*';}long long calculate(long long first, char op, long long second) {if (op == '+') {return first + second;} else if (op == '-') {return first - second;} else if (op == '*') {return first * second;}return 0;}int main() {char line[100];fgets(line, sizeof(line), stdin);long long res = 0;int maxLen = 0;int len = strlen(line);for (int i = 0; i < len; i++) {if (len - i <= maxLen) {break;}for (int j = i; j  maxLen) {maxLen = j + 1 - i;res = calculate(first, op, second);}}}printf("%lld\n", res);return 0;}

Node.js版本

const readline = require('readline');const rl = readline.createInterface({input: process.stdin,output: process.stdout});function isOperator(c) {return c === '+' || c === '-' || c === '*';}function calculate(first, op, second) {if (op === '+') {return first + second;} else if (op === '-') {return first - second;} else if (op === '*') {return first * second;}return 0;}rl.question('Enter a line: ', (line) => {let res = 0;let maxLen = 0;const len = line.length;for (let i = 0; i < len; i++) {if (len - i <= maxLen) {break;}for (let j = i; j  maxLen) {maxLen = j + 1 - i;const first = parseInt(match[1]);const op = match[2];const second = parseInt(match[3]);res = calculate(first, op, second);}}}console.log(res);rl.close();});