1 条题解
-
0
解题思路
这道题要求解一元线性方程,即形如
ax + b = cx + d
的方程。我们需要解析输入的字符串,计算左右两边的系数和常数项,然后判断解的情况:-
解析方程
- 将方程拆分为左边(Left Expression)和右边(Right Expression)。
- 分别计算左边的
x
的系数a
和常数项b
,以及右边的x
的系数c
和常数项d
。
-
合并同类项
- 将方程转化为
(a - c)x = (d - b)
的形式。
- 将方程转化为
-
判断解的情况
- 唯一解:如果
(a - c) != 0
,则解为x = (d - b) / (a - c)
。 - 无解:如果
(a - c) == 0
且(d - b) != 0
,则方程无解。 - 无限解:如果
(a - c) == 0
且(d - b) == 0
,则方程有无限多解。
- 唯一解:如果
-
输出结果
- 根据计算结果输出对应的解情况。
c++代码
#include <iostream> #include <string> #include <cctype> #include <cmath> #include <iomanip> #include <sstream> #include <stdexcept> using namespace std; struct Term { double coef; // x的系数 double constant; // 常数项 Term() : coef(0), constant(0) {} Term(double c, double k) : coef(c), constant(k) {} Term& operator+=(const Term& rhs) { coef += rhs.coef; constant += rhs.constant; return *this; } Term& operator-=(const Term& rhs) { coef -= rhs.coef; constant -= rhs.constant; return *this; } Term operator*(const Term& rhs) const { // 根据题目保证,不会出现x*x的情况 if (coef != 0 && rhs.coef != 0) { throw runtime_error("Non-linear term encountered"); } return Term(coef * rhs.constant + constant * rhs.coef, constant * rhs.constant); } }; class Parser { string expr; size_t pos; char peek() const { return pos < expr.size() ? expr[pos] : '\0'; } char get() { return pos < expr.size() ? expr[pos++] : '\0'; } void skipWhitespace() { while (peek() == ' ') get(); } Term parseExpression() { Term term = parseTerm(); while (true) { skipWhitespace(); char op = peek(); if (op == '+' || op == '-') { get(); Term rhs = parseTerm(); if (op == '+') term += rhs; else term -= rhs; } else { break; } } return term; } Term parseTerm() { Term term = parseFactor(); while (true) { skipWhitespace(); if (peek() == '*') { get(); Term rhs = parseFactor(); term = term * rhs; } else { break; } } return term; } Term parseFactor() { skipWhitespace(); if (peek() == '(') { get(); Term term = parseExpression(); skipWhitespace(); if (get() != ')') throw runtime_error("Missing ')'"); return term; } else if (peek() == 'x') { get(); return Term(1, 0); } else if (isdigit(peek())) { double num = parseNumber(); return Term(0, num); } else { throw runtime_error("Unexpected character"); } } double parseNumber() { double num = 0; while (isdigit(peek())) { num = num * 10 + (get() - '0'); } return num; } public: Parser(const string& s) : expr(s), pos(0) {} Term parse() { return parseExpression(); } }; int main() { string equation; int caseNum = 1; while (getline(cin, equation)) { size_t equalPos = equation.find('='); if (equalPos == string::npos) { cout << "Equation #" << caseNum++ << endl; cout << "Invalid equation format" << endl << endl; continue; } string leftExpr = equation.substr(0, equalPos); string rightExpr = equation.substr(equalPos + 1); try { Term left = Parser(leftExpr).parse(); Term right = Parser(rightExpr).parse(); double coef = left.coef - right.coef; double constTerm = right.constant - left.constant; cout << "Equation #" << caseNum++ << endl; if (fabs(coef) < 1e-9) { if (fabs(constTerm) < 1e-9) { cout << "Infinitely many solutions." << endl; } else { cout << "No solution." << endl; } } else { double x = constTerm / coef; cout << "x = " << fixed << setprecision(6) << x << endl; } } catch (const exception& e) { cout << "Equation #" << caseNum++ << endl; cout << "Error: " << e.what() << endl; } cout << endl; } return 0; }
-
- 1
信息
- ID
- 1253
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 3
- 已通过
- 1
- 上传者