50分求助

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

wangzixiao12345 @ 2024-04-11 21:42:49

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

感觉自己越写越乱QWQ


by canwen @ 2024-05-04 18:10:03

我已开始也50分,模拟题这种就重新审题就能A掉的(别写太复杂。

#include<iostream>
#include<cmath>
using namespace std;
int main(){
//  freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
    int n , a;cin>>n;
    for(int i=n;i>=0;i--){
        //i表示次数
        cin >> a;
        if(a==0) continue;
        if(a<0) cout<<"-";
        else if(i!=n) cout<<"+";
        if(i>0&&abs(a)==1)  ; else cout<<abs(a);
        if(i==1)cout<<"x";
        else if(i>1) cout<<"x^"<<i;
    }
    return 0;
}

by canwen @ 2024-05-04 18:10:36

马蜂良好((


by I_wanna_AK_IOI @ 2024-07-07 18:04:00

要认真审题,考虑一次项应是nx而不是nx^1

#include <bits/stdc++.h>
using namespace std;

int n;
int a[150];

string f(int x)
{
    string s = to_string(x);

    if (s == "0")
        return "";
    else if (s == "-1")
        return "-x";
    else if (s == "1")
        return "x";
    return s + "x";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for (int i = 1; i <= n + 1; i++)
    {
        cin >> a[i];
    }

    for (int i = 1; i <= n; i++)
    {
        if (f(a[i]) != "")
        {
            if (i != n)
            {
                cout << f(a[i]) << "^" << n + 1 - i;
            }
            else
                cout << f(a[i]);
        }

        if (to_string(a[i + 1])[0] != '-')
        {

            if (a[i + 1] != 0)
            {
                cout << '+';
            }
        }
    }

    // if (to_string(a[n+1])[0] == '-') cout << '-';
    // else cout << '+';
    if (a[n + 1] != 0)
        cout << a[n + 1];

    return 0;
}

by I_wanna_AK_IOI @ 2024-07-07 18:04:11

@wangzixiao12345


|