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 ggpw_XNW @ 2024-02-23 14:10:41

@lucy2012

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

by I_Love_DS @ 2024-02-23 14:27:23

#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(a==-1 && i) cout<<"-";      
                if(abs(a)>1 || i==0) cout<<a;    
                if(i>1) cout<<"x^"<<i;    
                if(i==1) cout<<"x";  
        } 
    }
    return 0;
}

by lucy2012 @ 2024-02-23 14:33:37

@User1025109 @liuruiqing 能不能稍稍解释一下?QAQ


by I_Love_DS @ 2024-02-23 14:36:00

测试点 #4

输入:

99
-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 1 8

正确输出:

-x^99-7x^50+100x^2+x+8

你的输出:

-x^99-7x^50+100x^21x+8

by lutaoquan2012 @ 2024-02-23 14:36:03

@liuruiqing 您们的真简单,我的好长呀

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,a[110];
int main(){
    cin>>n; 
    for(int i=n;i>=0;i--) cin>>a[i];
    for(int i=n;i>=0;i--){
        if(a[i]==0) continue;
        if(a[i]>=1&&i!=n) cout<<"+";
        if(a[i]>=2&&i!=0&&i!=1) cout<<a[i]<<"x^"<<i;
        if(a[i]<-1&&i!=0&&i!=1) cout<<a[i]<<"x^"<<i;  
        if(i==0&&a[i]>=1) cout<<a[i];
        if(i==0&&a[i]<0) cout<<a[i];
        if(a[i]==-1&&i!=0&&i!=1) cout<<"-x^"<<i;
        if(a[i]==1&&i!=0&&i!=1) cout<<"x^"<<i;
        if(i==1&&a[i]!=-1&&a[i]!=1) cout<<a[i]<<"x";
        if(a[i]==1&&i==1) cout<<"x";
        if(a[i]==-1&&i==1) cout<<"x";
    }
    return 0;
}/*5 
100 -1 1 -3 0 10
*/

by 编码落寞 @ 2024-02-23 14:55:38

@lucy2012

if(i==1){
    cout<<abs(a[i])<<"x";
}

这里为什么取绝对值没看懂


by lucy2012 @ 2024-02-23 15:00:16

@编码落寞 。。。。。。复制时手误,谢谢


by lucy2012 @ 2024-02-23 15:00:46

@liuruiqing 明白一点了


by lucy2012 @ 2024-02-23 15:01:54

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

80分了


by I_Love_DS @ 2024-02-23 15:03:59

#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){ //a 不为 0  
            if(i!=n && a>0) cout<<"+"; // 非首项 且 系数为正,输出+
            if(a==-1 && i) cout<<"-"; // 系数为-1 且 项数不为0,输出-     
                if(abs(a)>1 || i==0) cout<<a;   //a 不等于 -1,0,1 或 项数为0,输出a
                if(i>1) cout<<"x^"<<i;//项数大于1,输出x^项数    
                if(i==1) cout<<"x"; //项数等于1,输出x 
        } 
    }
    return 0;
}

| 下一页