当前位置:   article > 正文

提取字符串中的最长数学表达式并计算(67%用例) C卷(Java&&Python&&C++&&Node.js&&C语言)_4.提取字符串中的最长数学表达式并计算

4.提取字符串中的最长数学表达式并计算

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

输入描述:

字符串<

输出描述:

表达式值

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

输入

1-2abcd

输出

-1

Java版本

  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. String line = sc.nextLine();
  8. long res = 0;
  9. int maxLen = 0;
  10. int len = line.length();
  11. for (int i = 0; i < len; i++) {
  12. if (len - i <= maxLen) {
  13. break;
  14. }
  15. for (int j = i; j < len; j++) {
  16. String sub = line.substring(i, j + 1);
  17. Matcher matcher = Pattern.compile("(\\d+)([*+-])(\\d+)").matcher(sub);
  18. if (matcher.find() && j + 1 - i > maxLen) {
  19. maxLen = j + 1 - i;
  20. long first = Long.parseLong(matcher.group(1));
  21. String op = matcher.group(2);
  22. long second = Long.parseLong(matcher.group(3));
  23. switch (op) {
  24. case "+":
  25. res = first + second;
  26. break;
  27. case "-":
  28. res = first - second;
  29. break;
  30. case "*":
  31. res = first * second;
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. System.out.println(res);
  38. }
  39. }

Python版本

  1. import re
  2. line = input()
  3. res = 0
  4. max_len = 0
  5. length = len(line)
  6. for i in range(length):
  7. if length - i <= max_len:
  8. break
  9. for j in range(i, length):
  10. sub = line[i:j+1]
  11. match = re.search(r'(\d+)([*+-])(\d+)', sub)
  12. if match and j + 1 - i > max_len:
  13. max_len = j + 1 - i
  14. first = int(match.group(1))
  15. op = match.group(2)
  16. second = int(match.group(3))
  17. if op == '+':
  18. res = first + second
  19. elif op == '-':
  20. res = first - second
  21. elif op == '*':
  22. res = first * second
  23. print(res)

C++版本

  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4. using namespace std;
  5. int main() {
  6. string line;
  7. getline(cin, line);
  8. long long res = 0;
  9. int maxLen = 0;
  10. int len = line.length();
  11. for (int i = 0; i < len; i++) {
  12. if (len - i <= maxLen) {
  13. break;
  14. }
  15. for (int j = i; j < len; j++) {
  16. string sub = line.substr(i, j - i + 1);
  17. smatch match;
  18. regex pattern("(\\d+)([*+-])(\\d+)");
  19. if (regex_search(sub, match, pattern) && j + 1 - i > maxLen) {
  20. maxLen = j + 1 - i;
  21. long long first = stoll(match[1]);
  22. string op = match[2];
  23. long long second = stoll(match[3]);
  24. if (op == "+") {
  25. res = first + second;
  26. } else if (op == "-") {
  27. res = first - second;
  28. } else if (op == "*") {
  29. res = first * second;
  30. }
  31. }
  32. }
  33. }
  34. cout << res << endl;
  35. return 0;
  36. }

C语言版本

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. bool isOperator(char c) {
  5. return c == '+' || c == '-' || c == '*';
  6. }
  7. long long calculate(long long first, char op, long long second) {
  8. if (op == '+') {
  9. return first + second;
  10. } else if (op == '-') {
  11. return first - second;
  12. } else if (op == '*') {
  13. return first * second;
  14. }
  15. return 0;
  16. }
  17. int main() {
  18. char line[100];
  19. fgets(line, sizeof(line), stdin);
  20. long long res = 0;
  21. int maxLen = 0;
  22. int len = strlen(line);
  23. for (int i = 0; i < len; i++) {
  24. if (len - i <= maxLen) {
  25. break;
  26. }
  27. for (int j = i; j < len; j++) {
  28. char sub[100];
  29. strncpy(sub, &line[i], j - i + 1);
  30. sub[j - i + 1] = '\0';
  31. long long first, second;
  32. char op;
  33. int matched = sscanf(sub, "%lld%c%lld", &first, &op, &second);
  34. if (matched == 3 && isOperator(op) && j + 1 - i > maxLen) {
  35. maxLen = j + 1 - i;
  36. res = calculate(first, op, second);
  37. }
  38. }
  39. }
  40. printf("%lld\n", res);
  41. return 0;
  42. }

Node.js版本

  1. const readline = require('readline');
  2. const rl = readline.createInterface({
  3. input: process.stdin,
  4. output: process.stdout
  5. });
  6. function isOperator(c) {
  7. return c === '+' || c === '-' || c === '*';
  8. }
  9. function calculate(first, op, second) {
  10. if (op === '+') {
  11. return first + second;
  12. } else if (op === '-') {
  13. return first - second;
  14. } else if (op === '*') {
  15. return first * second;
  16. }
  17. return 0;
  18. }
  19. rl.question('Enter a line: ', (line) => {
  20. let res = 0;
  21. let maxLen = 0;
  22. const len = line.length;
  23. for (let i = 0; i < len; i++) {
  24. if (len - i <= maxLen) {
  25. break;
  26. }
  27. for (let j = i; j < len; j++) {
  28. const sub = line.substring(i, j + 1);
  29. const match = sub.match(/(\d+)([*+-])(\d+)/);
  30. if (match && j + 1 - i > maxLen) {
  31. maxLen = j + 1 - i;
  32. const first = parseInt(match[1]);
  33. const op = match[2];
  34. const second = parseInt(match[3]);
  35. res = calculate(first, op, second);
  36. }
  37. }
  38. }
  39. console.log(res);
  40. rl.close();
  41. });

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/949401
推荐阅读
相关标签
  

闽ICP备14008679号