感觉能特判的都判了,样例能过,但只过2个点

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

loegoo @ 2024-07-18 14:30:58

#include <bits/stdc++.h>
using namespace std;
int a[101];

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n + 1; i++) {
        cin >> a[i];
    }
    if (n == 0) {
        cout << a[1];
        return 0;
    }
    for (int i = 1; i < n + 1; i++) {
        if (i == 1) {
            if (a[i] > 1)
                cout << a[i] << "x^" << n + 1 - i;
            else if (a[i] < -1)
                cout <<  a[i] << "x^" << n + 1 - i;
            else if (a[i] == 1)
                cout << "x^" << n + 1 - i;
            else if (a[i] == -1)
                cout << "-" << "x^" << n + 1 - i;
        }
        if (i > 1) {
            if (a[i] > 1)
                cout << "+" << a[i] << "x^" << n + 1 - i;
            else if (a[i] < -1)
                cout <<  a[i] << "x^" << n + 1 - i;
            else if (a[i] == 1)
                cout << "+" << "x^" << n + 1 - i;
            else if (a[i] == -1)
                cout << "-" << "x^" << n + 1 - i;
        }
    }
    if (a[n + 1] > 0)
        cout << "+" << a[n + 1];
    if (a[n + 1] < 0)
        cout << "-" << a[n + 1];
    return 0;
}

by ll_dio @ 2024-07-18 14:33:58

一次项不是x^1而是x


by ll_dio @ 2024-07-18 14:34:20

所以a[n]也要单拿出来搞


by ll_dio @ 2024-07-18 14:34:39

样例刚好都没有一次项


by ll_dio @ 2024-07-18 14:43:16

@loegoo


by loegoo @ 2024-07-18 16:13:03

谢谢哥


|