70分求助
查看原帖
70分求助
1372296
Chorsty楼主2025/1/16 20:53
#include <bits/stdc++.h>
using namespace std;

int main()
{
  bool flag = 0 ,f=1;
  int n ,x;
  cin >> n;
  for (int i=0 ,j=n; i<=n; i++ ,j--)
  {
    cin >> x;
    if (x == -1) cout << '-' << "x^" << j;
    else
    {
      if (x == 0) continue;
      if (j == 0)
      {
        if (x > 0 && !f) cout << '+';
        if (x < 0 && !f) cout << '-';
        cout << abs(x);
        if (j != 1 && j != 0) cout << "^" << j;
        continue;
      }
      if (x > 0 && f)
      {
        f = 0;
        if (x != 1) cout << x;
        cout << "x^";
        if (j != 1) cout << j;
        continue;
      }
      else if (x < 0 && f)
      {
        f = 0;
        if (x != 1) cout << x;
        cout << "x^" << j;
        continue;
      }

      if (x < 0) flag = 1;
      else flag = 0;

      x = abs(x);
      if (flag == 1) cout << '-';
      else cout << '+';

      if (x == 1) cout << "x";
      else cout << x << "x";
      if (j != 1 && j != 0) cout << "^" << j;
      if (flag == 1) flag = 0;
      else flag = 1;
    }
  }
  return 0;
}
2025/1/16 20:53
加载中...