题目:多项式输出
问题编号:516
题目描述
一元 n 次多项式可用如下的表达式表示:
其中,a_i·x^i 称为i次项,a_i称为i 次项的系数。给出一个一元多项式各项的次数和系
数,请按照如下规定的格式要求输出该多项式:
1. 多项式中自变量为x,从左到右按照次数递减顺序给出多项式。
2. 多项式中只包含系数不为0 的项。
3. 如果多项式n 次项系数为正,则多项式开头不出现“+”号,如果多项式n 次项系
数为负,则多项式以“-”号开头。
4. 对于不是最高次的项,以“+”号或者“-”号连接此项与前一项,分别表示此项
系数为正或者系数为负。紧跟一个正整数,表示此项系数的绝对值(如果一个高于0 次的项,
其系数的绝对值为1,则无需输出1)。如果x 的指数大于1,则接下来紧跟的指数部分的形
式为“x^b”,其中b 为x 的指数;如果x 的指数为1,则接下来紧跟的指数部分形式为“x”;
如果x 的指数为0,则仅需输出系数即可。
5. 多项式中,多项式的开头、结尾不含多余的空格。
【数据范围】
1 ≤ n ≤ 100,多项式各次项系数的绝对值均不超过100。
输入格式
共有2 行。
第一行 1 个整数,n,表示一元多项式的次数。
第二行有 n+1 个整数,其中第i 个整数表示第n-i+1 次项的系数,每两个整数之间用空
输出格式
共1 行,按题目所述格式输出多项式。
样例输入
【输入样例1】
5
100 -1 1 -3 0 10
【输入样例2】
3
-50 0 0 1
样例输出
【输出样例1】
100x^5-x^4+x^3-3x^2+10
【输出样例2】
-50x^3+1
共有几个问题需要注意:
每一个数字是否为1或-1。
第一个数字前方的符号。
倒数第二个数字的系数为0就不需要输出。
最后一个数字的符号。
Rqnoj&Tyvj双测试通过:
VijosNT Mini 2.0.5.6
Free Pascal Compiler version 2.6.0 [2011/12/25] for i386
Copyright (c) 1993-2011 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling foo.pas
foo.pas(5,7) Note: Local variable "temp" not used
Linking foo.exe
55 lines compiled, 0.1 sec , 30128 bytes code, 1660 bytes data
1 note(s) issued
#01: Accepted (0ms, 580KB)
#02: Accepted (0ms, 580KB)
#03: Accepted (0ms, 580KB)
#04: Accepted (0ms, 580KB)
#05: Accepted (0ms, 580KB)
#06: Accepted (0ms, 580KB)
#07: Accepted (0ms, 580KB)
#08: Accepted (0ms, 580KB)
#09: Accepted (0ms, 580KB)
#10: Accepted (0ms, 580KB)
Accepted / 100 / 0ms / 580KB
状态: | Accepted |
测评机: | Xeond[6] |
得分: | 100分 [我要评价一下题目~] |
提交日期: | 2012-6-8 18:54:00 |
有效耗时: | 1579毫秒 |
测试结果1: | 通过本测试点|有效耗时172ms |
测试结果2: | 通过本测试点|有效耗时157ms |
测试结果3: | 通过本测试点|有效耗时156ms |
测试结果4: | 通过本测试点|有效耗时156ms |
测试结果5: | 通过本测试点|有效耗时156ms |
测试结果6: | 通过本测试点|有效耗时157ms |
测试结果7: | 通过本测试点|有效耗时156ms |
测试结果8: | 通过本测试点|有效耗时156ms |
测试结果9: | 通过本测试点|有效耗时156ms |
测试结果10: | 通过本测试点|有效耗时157ms |
Program Rqnoj_516;
var n:longint;
coe,num:array[1..100]of longint;
procedure init;
var i,temp:longint;
begin
readln(n);
fillchar(coe,sizeof(coe),0);
fillchar(num,sizeof(num),0);
for i:=1 to n do
begin
read(coe[i]);
num[i]:=n-i+1;
end;
read(coe[n+1]);
end;
procedure main;
var i,temp:longint;
begin
for i:=1 to n do
if (coe[i]<>0) then begin temp:=i; break; end;
i:=temp;
if (coe[i]<>0)then
begin
if((coe[i]<0)and(coe[i]<>-1)) then write(coe[i],'x^',num[i])
else if (coe[i]=-1) then write('-x^',num[i])
else if((coe[i]<>1)and(coe[i]>0))then write(coe[i],'x^',num[i])
else if (coe[i]=1) then write('x^',num[i]);
end;
for i:= temp+1 to n-1 do
begin
if (coe[i]<>0)then
begin
if((coe[i]<0)and(coe[i]<>-1)) then write(coe[i],'x^',num[i])
else if (coe[i]=-1) then write('-x^',num[i])
else if((coe[i]<>1)and(coe[i]>0))then write('+',coe[i],'x^',num[i])
else if (coe[i]=1) then write('+x^',num[i]);
end;
end;
i:=n;
if (coe[i]<>0)then
begin
if((coe[i]<0)and(coe[i]<>-1)) then write(coe[i],'x')
else if (coe[i]=-1) then write('-x')
else if((coe[i]<>1)and(coe[i]>0))then write('+',coe[i],'x')
else if (coe[i]=1) then write('+x');
end;
if ((coe[n+1]<>0)and(coe[n+1]>0)) then write('+',coe[n+1])else if (coe[n+1]<>0) then write(coe[n+1]);
end;
begin
init;
main;
end.