P1067 [NOIP2009 普及组] 多项式输出

题目传送门

#include <bits/stdc++.h>

using namespace std;
const int N = 110;

int main() {
    int n;
    cin >> n;
    int x;
    // a[0~n-1]是系数,a[n]是常数
    for (int i = 0; i < n; i++) {
        cin >> x;
        //带x的前0->n-1位
        if (x < 0) {
            if (x == -1)cout << "-"; else cout << x;
        } else if (x > 0) {
            if (i > 0) cout << "+";
            if (x != 1) cout << x;
        } else continue;
        if (n - i > 1) cout << "x^" << n - i;
        else cout << "x";
    }

    //常数位
    cin >> x;
    if (x > 0) cout << "+" << x; else if (x < 0) cout << x;
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15588831.html