P1067 多项式输出

P1067 多项式输出

我恨模拟题(海星海星)真香again

题解

模拟题要注意很多细节

几组数据了解一下

 

 

 

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>

using namespace std;

int n;
struct node
{
    int xi;
    char hao;
    int zhi;
    
}a[105];

int main()
{
//    freopen("poly.in","r",stdin);
//    freopen("poly.out","w",stdout);
    scanf("%d",&n);
    int cnt=0;
    for(int i=n;i>=0;i--)
    {
        int k;
        scanf("%d",&k);
        if(k==0&&n!=0) continue;
        if(cnt==0)
        {
            cnt++;
            a[cnt].xi =k;
            a[cnt].hao ='no';
            a[cnt].zhi =i;
        }
        else
        {
            cnt++;
            if(k>0)
            {
                a[cnt].xi =k;
                a[cnt].hao ='+';
                a[cnt].zhi =i;
                
            }
            else
            {
                a[cnt].xi =k;
                a[cnt].hao ='ye';
                a[cnt].zhi =i;
            }
        } 
    }
    
    for(int i=1;i<=cnt;i++)
    {
        if(a[i].hao =='no')
        {
            printf("%dx^%d",a[i].xi ,a[i].zhi );
            
        } 
        else
        {
            if(a[i].hao =='+') printf("+"); 
            if(a[i].zhi ==1)
            {
                if(a[i].xi ==1) printf("x" );
                else if (a[i].xi ==-1) printf("-x" );
                else printf("%dx",a[i].xi );
             } 
            
            else if(a[i].zhi ==0)
            {
                printf("%d",a[i].xi );
            }
            
            else
            {
                if(a[i].xi ==1) printf("x^%d",a[i].zhi );
                else if (a[i].xi ==-1) printf("-x^%d" ,a[i].zhi);
                else
                printf("%dx^%d",a[i].xi ,a[i].zhi );
            
            }
            
        } 
    }
    
    return 0;
}

 

 

posted @ 2019-06-14 16:13  晔子  阅读(199)  评论(0编辑  收藏  举报