2 条题解

  • 1
    @ 2025-12-19 21:38:20

    #include #include #include using namespace std;

    int main() { string line; getline(cin, line);

    // 找到运算符位置
    int opPos = -1;
    char op;
    for (int i = 0; i < line.length(); i++) {
        if (line[i] == '+' || line[i] == '-' || line[i] == '*' || line[i] == '/' || line[i] == '%') {
            opPos = i;
            op = line[i];
            break;
        }
    }
    
    // 提取左边运算数
    string leftStr = line.substr(0, opPos);
    // 提取右边运算数
    string rightStr = line.substr(opPos + 1);
    
    // 去除前后空格
    leftStr.erase(0, leftStr.find_first_not_of(" "));
    leftStr.erase(leftStr.find_last_not_of(" ") + 1);
    rightStr.erase(0, rightStr.find_first_not_of(" "));
    rightStr.erase(rightStr.find_last_not_of(" ") + 1);
    
    int a = stoi(leftStr);
    int b = stoi(rightStr);
    
    int result;
    switch (op) {
        case '+': result = a + b; break;
        case '-': result = a - b; break;
        case '*': result = a * b; break;
        case '/': result = a / b; break;
        case '%': result = a % b; break;
        default: break;
    }
    
    cout << result << endl;
    
    return 0;
    

    }

    • 0
      @ 2025-12-22 11:50:44
      #include<bits/stdc++.h>
      using namespace std;
      int main(){
          int a,c;
          char b;
          cin>>a>>b>>c;
          if(b=='+')
          	cout<<a+c;
          else if(b=='-')
          	cout<<a-c;
          else if(b=='*')
          	cout<<a*c;
          else if(b=='/')
          	cout<<a/c;
          else
          	cout<<a%c;
      	return 0;
      }
      
      • 1

      信息

      ID
      6987
      时间
      1000ms
      内存
      512MiB
      难度
      1
      标签
      递交数
      7
      已通过
      7
      上传者