40pts,求调

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

ofthemoon @ 2024-10-01 16:01:43

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

by LIUHAOYU2013 @ 2024-10-03 22:07:16

@ofthemoon

#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;
}

by chengzg @ 2024-10-06 18:08:20


using namespace std;
int n;
int a;
int main()
{
    cin>>n;
    for(int i=n;i>=0;i--)
    {
        cin>>a;
        if(a!=0)
        {
            if(a>0 && i!=n) cout<<'+';
            if(abs(a)>1 || i==0) cout<<a;
            if(a==-1 && i) cout<<'-';
            if(i!=0 && i!=1) cout<<'x'<<'^'<<i;
            if(i==1) cout<<'x';
        }
    }
    return 0;
}

|