分别对 n+1
项进行判断,需要考虑的情况较多,有:
- 系数为
0
时,不需要输出该项; - 系数为正且该项不是第一个输出项时,需要输出
+
; - 系数为负时,需要输出
-
; - 系数绝对值为
1
且不为常数项时,不需要输出系数; - 常数项不需要输出
x
和指数。 - 非常数项指数为
1
时,不需要输出指数。
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
int a[n + 1];
for (int i = 0; i <= n; i++)
cin >> a[i];
bool flag = false;
for (int i = 0; i <= n; i++)
{
if (a[i] == 0)
continue;
if (a[i] > 0 && flag)
cout << "+";
if (a[i] < 0)
cout << "-";
if (abs(a[i]) != 1 || i == n)
cout << abs(a[i]);
if (i < n - 1)
cout << "x^" << n - i;
if (i == n - 1)
cout << "x";
flag = true;
}
cout << endl;
}
signed main()
{
ios::sync_with_stdio(false);
int t;
// cin >> t;
t = 1;
while (t--)
solve();
return 0;
}