题解(45行)
2025-06-24 10:06:47
发布于:江苏
#include<iostream>
#include<cmath>
using namespace std;
int a[105];
int main() {
int n;
cin >> n;
for (int i = 0; i <= n; i++)
cin >> a[i];
for (int i = 0; i < n; i++) {
if (a[i]) {
if (i != n - 1) {
if ((abs(a[i]) != 1) && (!i))
cout << a[i] << "x^" << n - i;
else if ((a[i] == -1) && (!i))
cout << "-" << "x^" << n - i;
else if ((a[i] == 1) && (!i))
cout << "x^" << n - i;
else if (a[i] > 1 && i)
cout << "+" << a[i] << "x^" << n - i;
else if (a[i] < -1 && i)
cout << a[i] << "x^" << n - i;
else if (a[i] == -1 && i)
cout << "-" << "x^" << n - i;
else
cout << "+" << "x^" << n - i;
}
if (i == n - 1) {
if (a[i] > 1)
cout << "+" << a[i] << "x";
else if (a[i] < -1)
cout << a[i] << "x";
else if (a[i] == 1)
cout << "+x";
else
cout << "-x";
}
}
}
if (a[n] > 0)
cout << "+" << a[n] << endl;
else if (a[n] < 0)
cout << a[n] << endl;
return 0;
}
这里空空如也
有帮助,赞一个