求求求求求,只输出了第一项

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

H_Monkey @ 2024-08-14 23:39:50

#include <bits/stdc++.h>
using namespace std;
int n;
int a[110];
int cnt = 2;
int main(){
    cin >> n;
    for(int i = 1; i <= n+1; i++)cin >> a[i];
    if(n > 1){
        cout << a[1] << "x" << "^" << n;
        n--;
    }
    while(n--){
        if(n == 1){
            if(a[cnt] == 0)return 0;
            cout << a[cnt];
            return 0;
        }
        if(a[cnt] < 0){
            if(a[cnt] == -1){
                cout << "-x" << "^" << n;
                cnt++;
            }else{
                cout << a[cnt] << "^" << n;
                cnt++;
            }
        }
        if(a[cnt] > 0){
            if(a[cnt] == 1){
                cout << "+x" << "^" << n;
                cnt++;
            }else{
                cout << "+" << a[cnt] << "x" << "^" << n;
                cnt++;
            }
        }
        if(a[cnt] == 0){
            cnt++;
        }
    }
}

by super_JuRuo_Z6H @ 2024-08-21 16:34:55

我的:

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

|