30分求助!!!

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

E_RONALDO @ 2023-10-12 15:01:21

#include<iostream>
using namespace std;
long long a[100010];
int main(){
    long long n,flag;
    cin>>n;
    flag=n;
    for(int i=0;i<=n;i++){
        cin>>a[i];
    }
    for(int i=0;i<=n;i++){
        if(a[i]==1){
            if(i==n){
                if(i>0){
                    cout<<'+'<<a[i];
                }
                else{
                    cout<<a[i];
                }

            }
            else{
                cout<<'+'<<'x'<<'^'<<flag;
            }

        }
        else if(a[i]==-1){
            cout<<'-'<<'x'<<'^'<<flag;
        }
        else if(a[i]==0){
            flag--;
            continue;
        }
        else {
            if(i!=0){
                if(i==n){
                    if(a[i]>0){
                        cout<<'+'<<a[i];
                    } 
                    else {
                        cout<<a[i];
                    }
                } else {
                    if(a[i]>0){
                        cout<<'+'<<a[i]<<'x'<<'^'<<flag;
                    } 
                    else {
                        cout<<a[i]<<'x'<<'^'<<flag;
                    }
                }
            }  
            else {
                cout<<a[i]<<'x'<<'^'<<flag;
            } 

        }

        flag--;
    }
    return 0;
}

by Hamdi @ 2023-10-14 12:13:18

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

by E_RONALDO @ 2023-10-19 17:32:57

谢谢大佬


|