50pts(?)求助

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

Greeper @ 2024-09-08 09:31:15

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    bool fir=1;
    for(int i=n;i>=0;i--)
    {
        int x;
        cin>>x;
        bool f=0;
        if(x<0)
        {
            f=1;
            x=-x;
        }
        if(x==0)
        {
            continue;
        }
        if(fir)
        {
            if(f)
            {
                cout<<'-';
            }
            if(x!=1||i==0)
            {
                cout<<x;
            }
            if(i)
                cout<<"x^"<<i;
            fir=0;
        }
        else
        {
            if(f)
            {
                cout<<'-';
            }
            else
            {
                cout<<'+';
            }
            if(x!=1||i==0)
            {
                cout<<x;
            }
            if(i)
                cout<<"x^"<<i;
        }
    }
    return 0;
}

by Lisuyang @ 2024-09-08 11:18:06

@Greeper

#include <iostream>
using namespace std;
int main(){
    int n,a;
    scanf("%d", &n);
    for(int i = n;i >= 0; -- i){
        scanf("%d", &a);
        if(a){  
            if(i != n && a > 0) printf("+");   
            if(abs(a) > 1 || i == 0) printf("%d", a); 
            if(a == -1 && i) printf("-");
            if(i > 1) printf("x^%d", i);
            if(i == 1) printf("x");  
        }
    }
    return 0;
}

|