已经弄了两天了

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

Cuimenghao @ 2024-02-09 09:50:09

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, a[102], b = 0;
    cin >> n;
    for (int i = 0; i <= n ; i++) {
        cin >> a[i];
    }
    for (int i = 0; i <= n - 1; i++) {
        if (a[i] == 0) {
            continue;
        } else if (a[i] == - 1) {
            cout << "-" << "x" << "^" << n - i;
            if (a[i + 1] > 0) {
                cout << "+";
            }
        } else if ( a[i] < 0 && i != - 1) {
            cout << a[i] << "x" << "^" << n - i;
            if (a[i + 1] > 0) {
                cout << "+";
            }
        } else if (a[i] == 1) {
            cout << "x" << "^" << n - i;
            if (a[i + 1] > 0) {
                cout << "+";
            }
        } else {
            cout << a[i] << "x" << "^" << n - i ;
            if (a[i + 1] > 0) {
                cout << "+";
            }
        }
        b = i;
    }
    if (a[n]>0||a[n]<0) {
        cout << a[n];
    }

    return 0;
}

by __Tonycyt__ @ 2024-02-09 10:06:07

然后,所有的 if (a[i + 1] > 0) 改成 if (a[i + 1] > 0 && i != n-1)


by Cuimenghao @ 2024-02-09 10:10:55

欧克


by __Tonycyt__ @ 2024-02-09 10:13:23

一番大改之后,变成了这样,现在是50分

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, a[102], b = 0;
    cin >> n;

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

    return 0;
}

by __Tonycyt__ @ 2024-02-09 10:14:51

// 开头的行表示注释,编译器会跳过这一整行


by chenyuchen_1 @ 2024-02-09 10:15:17

@Cuimenghao i=n-1的时候

5 
100 -1 1 -3 20 10

by Cuimenghao @ 2024-02-09 10:15:27

我看看


by __Tonycyt__ @ 2024-02-09 10:16:26

又改了一下,AC了

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, a[102], b = 0;
    cin >> n;

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

    return 0;
}

by __Tonycyt__ @ 2024-02-09 10:17:12

@Cuimenghao


by Cuimenghao @ 2024-02-09 10:19:39

谢谢


上一页 |