题目描述
一元nn次多项式可用如下的表达式表示:
给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式:
-
多项式中自变量为x,从左到右按照次数递减顺序给出多项式。
-
多项式中只包含系数不为0的项。
-
如果多项式n次项系数为正,则多项式开头不出现“+”号,如果多项式n次项系数为负,则多项式以“-”号开头。
对于不是最高次的项,以“+”号或者“-”号连接此项与前一项,分别表示此项系数为正或者系数为负。紧跟一个正整数,表示此项系数的绝对值(如果一个高于0次的项,其系数的绝对值为11,则无需输出 11)。如果x的指数大于11,则接下来紧跟的指数部分的形式为“xb”,其中 bb为 x的指数;如果 x的指数为11,则接下来紧跟的指数部分形式为“x”;如果 xx 的指数为0,则仅需输出系数即可。
多项式中,多项式的开头、结尾不含多余的空格。
输入格式
输入共有 2 行
第一行1 个整数,n,表示一元多项式的次数。
第二行有 n+1个整数,其中第i个整数表示第n−i+1 次项的系数,每两个整数之间用空格隔开。
输出格式
输出共 11 行,按题目所述格式输出多项式。
输入输出样例
输入 #1
5 100 -1 1 -3 0 10
输出 #1
100x^5-x^4+x^3-3x^2+10
输入 #2
3 -50 0 0 1
输出 #2
-50x^3+1
说明/提示
NOIP 2009 普及组 第一题
对于100%数据,0≤n≤100,−100≤系数≤100
什么都不说了。一万个坑。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } int main() { int a; int n; int temp=0; a=read(); int l=a; for(int i=0;i<=a;i++) { n=read(); if(n==0) { l--; continue; } if(l==0&&n!=0) { if(n>0) cout<<"+"; cout<<n; break; } if(temp==0) { if(n==1) { cout<<"x^"<<l; l--; temp=1; continue; } if(n==-1) { cout<<"-x^"<<l; l--; temp=1; continue; } printf("%dx^%d",n,l); l--; temp=1; continue; } if(l==1&&n!=0) { if(n==1) { cout<<"+x"; l--; continue; } if(n==-1) { cout<<"-x"; l--; continue; } if(n>0) cout<<"+"; cout<<n<<"x"; l--; continue; } if(n==1) { printf("+x^%d",l); l--; continue; } if(n==-1) { printf("-x^%d",l); l--; continue; } if(n>0) printf("+"); printf("%dx^%d",n,l); l--; } if(temp==0) cout<<0; return 0; }