This answer quotes ChatGPT
Refer to the code below
#include
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
vector<int> coefficient(n+1, 0);
for (int i = 0; i <= n; i++) {
cin >> coefficient[i];
}
bool is_first = true;
for (int i = n; i >= 0; i--) {
if (coefficient[i] != 0) {
if (is_first) {
is_first = false;
if (coefficient[i] == -1) {
cout << "-";
} else if (coefficient[i] != 1) {
cout << coefficient[i];
}
} else {
if (coefficient[i] < 0) {
cout << " - ";
if (coefficient[i] == -1) {
cout << "x";
} else {
cout << -coefficient[i] << "x";
}
} else {
cout << " + ";
if (coefficient[i] == 1) {
cout << "x";
} else {
cout << coefficient[i] << "x";
}
}
}
if (i > 1) {
cout << "^" << i;
} else if (i == 1) {
cout << "x";
}
}
}
cout << endl;
return 0;
}