0
Follow
3
View

How to solve the problem of #C++#?

dr_heqing 注册会员
2023-02-26 00:04

This answer quotes ChatGPT

Refer to the code below

#include 
#include 
#include 

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> coefficient(n+1, 0);
    for (int i = 0; i <= n; i++) {
        cin >> coefficient[i];
    }
    bool is_first = true;
    for (int i = n; i >= 0; i--) {
        if (coefficient[i] != 0) {
            if (is_first) {
                is_first = false;
                if (coefficient[i] == -1) {
                    cout << "-";
                } else if (coefficient[i] != 1) {
                    cout << coefficient[i];
                }
            } else {
                if (coefficient[i] < 0) {
                    cout << " - ";
                    if (coefficient[i] == -1) {
                        cout << "x";
                    } else {
                        cout << -coefficient[i] << "x";
                    }
                } else {
                    cout << " + ";
                    if (coefficient[i] == 1) {
                        cout << "x";
                    } else {
                        cout << coefficient[i] << "x";
                    }
                }
            }
            if (i > 1) {
                cout << "^" << i;
            } else if (i == 1) {
                cout << "x";
            }
        }
    }
    cout << endl;
    return 0;
}


About the Author

Question Info

Publish Time
2023-02-26 00:04
Update Time
2023-02-26 00:04