题解
2025-07-06 14:02:28
发布于:广东
1阅读
0回复
0点赞
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> coefficients(n + 1);
for (int i = 0; i <= n; ++i) {
cin >> coefficients[i];
}
string result;
bool first_term = true;
for (int degree = n; degree >= 0; --degree) {
int coeff = coefficients[n - degree];
if (coeff == 0) {
continue;
}
// Handle sign
string sign;
if (first_term) {
if (coeff < 0) {
sign = "-";
}
first_term = false;
} else {
sign = (coeff > 0) ? "+" : "-";
}
// Handle coefficient
string coeff_str;
int abs_coeff = abs(coeff);
if (abs_coeff != 1 || degree == 0) {
coeff_str = to_string(abs_coeff);
}
// Handle variable part
string var_part;
if (degree > 1) {
var_part = "x^" + to_string(degree);
} else if (degree == 1) {
var_part = "x";
}
// Combine all parts
result += sign + coeff_str + var_part;
}
cout << result << endl;
return 0;
}
这里空空如也
有帮助,赞一个