50小垃圾

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

lucy2012 @ 2024-02-23 14:03:36

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

by lucy2012 @ 2024-02-23 15:06:36

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

90分


by lucy2012 @ 2024-02-23 15:07:09

就测试点4过不去


by lucy2012 @ 2024-02-23 15:08:20

@liuruiqing 代码看明白了


by I_Love_DS @ 2024-02-23 15:20:26

整码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,a[1000];
    cin>>n;
    if(n==0){
        cin>>a[1];
        cout<<a[1];
        return 0;
    }
    for(int i=n;i>=0;i--)
        cin>>a[i];
    for(int i=n;i>=0;i--){
        if(a[i]==0)
            continue;

        if(i==1){
            if(a[i]>=1)
                    cout<<"+";
                else cout << "-";
            if (abs(a[i]) != 1)cout<<a[i]<<"x";
                else cout << "x";
        }else if(i==n){
            if(a[i]<0)
                cout<<"-";
            if(a[i]==1||a[i]==-1)
                cout<<"x^"<<n;
            else
                cout<<abs(a[i])<<"x^"<<n;
        }else if(i==0){
            if(a[i]<0)
                cout<<"-";
            else
                cout<<"+";
            cout<<abs(a[i]);
        }else{
            if(a[i]<0)
                cout<<"-";
            else
                cout<<"+";
            if(a[i]==1||a[i]==-1)
                cout<<"x^"<<i;
            else
                cout<<abs(a[i])<<"x^"<<i;
        }
    }
    return 0;
}

by I_Love_DS @ 2024-02-23 15:21:01

@lucy2012


by 编码落寞 @ 2024-02-23 15:24:42

@lucy2012

        }else if(i==0){
            if(a[i]<0)
                cout<<"-";
            else
                cout<<"+";
            cout<<abs(a[i]);
        }

等于0的情况处理错误


上一页 |