问题描述
给出多项式的最高次项,再给出各次项的系数,要求将此多项式补全。
样例输入
5
100 -1 1 -3 0 10
样例输出
100x^5-x^4+x^3-3x^2+10
算法讨论
这道题不难,纯粹的模拟。但是它是一个巨坑!巨坑!!首先需要注意的是系数的绝对值为1时,不能输出1x,要输出x,还有就是指数为1时只能输出x,不能输出x^1,还有种情况就是当系数为负数时不要多加一个负号。基本要注意的就这些。时间复杂度O(n)。
const
maxn=101;
var
a:array[1..maxn] of longint;
i,j,n,m:longint;
st,st1,st2:string;
begin
read(n);
for i:=1 to n+1 do
read(a[i]);
if n=0
then halt;
m:=n;
str(a[1],st1);
str(m,st2);
if a[1]=1
then st:='x^'+st2
else if a[1]=-1
then st:='-x^'+st2
else st:=st1+'x^'+st2;
dec(m);
for i:=2 to n do
begin
if a[i]>0
then if a[i]<>1
then begin
str(a[i],st1);
str(m,st2);
if m=1
then st:=st+'+'+st1+'x'
else st:=st+'+'+st1+'x^'+st2;
dec(m)
end
else begin
str(m,st2);
if m=1
then st:=st+'+x'
else st:=st+'+x^'+st2;
dec(m)
end;
if a[i]<0
then if a[i]<>-1
then begin
str(a[i],st1);
str(m,st2);
if m=1
then st:=st+st1+'x'
else st:=st+st1+'x^'+st2;
dec(m)
end
else begin
str(m,st2);
if m=1
then st:=st+'-x'
else st:=st+'-x^'+st2;
dec(m)
end;
if a[i]=0
then dec(m)
end;
str(a[n+1],st1);
if a[n+1]>0
then st:=st+'+'+st1;
if a[n+1]<0
then st:=st+st1;
writeln(st)
end.
Pixiv ID:60209427