提交时间:2026-02-05 16:24:50
运行 ID: 84022
#include <iostream> #include <vector> #include <string> using namespace std; int main() { int n; cin >> n; vector<int> coefficients(n + 1); for (int i = 0; i <= n; ++i) { cin >> coefficients[i]; } vector<string> terms; for (int i = 0; i <= n; ++i) { int power = n - i; int coeff = coefficients[i]; if (coeff == 0) continue; string term; // 处理符号 if (coeff > 0) { if (!terms.empty()) { term += "+"; } } else { term += "-"; } // 处理系数绝对值 int abs_coeff = abs(coeff); if (abs_coeff != 1 || power == 0) { term += to_string(abs_coeff); } // 处理指数部分 if (power > 1) { term += "x^" + to_string(power); } else if (power == 1) { term += "x"; } terms.push_back(term); } // 拼接所有项 string result; for (const string& term : terms) { result += term; } cout << result << endl; return 0; }