多项式输出
问题转述:
给出一个一元多项式各项的次数和系数,按照规定的格式要求输出该多项式。
分析:
普及组的水题。多项式大家应该很熟悉,输出的时候注意一下几点即可:
1. 最高次项为正的话开头无加号。
2. 系数为0不输出。
3. 一次项输出x,并非x^1。
4. 非常数项系数为1或-1时直接输出正负号,但是常数项需要输出该数字。
其中除第三项外其它均可在样例中检查出错误,但是若没想到第三点那么就只能得到50分了。
程序:
var i,k,n:longint;
begin
assign(input,'poly.in');reset(input);
assign(output,'poly.out');rewrite(output);
readln(n);
for i:=n downto 0 do
begin
read(k);
if k=0 then continue;
if (k>0) and (i<>n) then write('+');
if i=0 then write(k)
else if (abs(k)<>1) then write(k) else if k=-1 then write('-');
if i<>0 then
if i=1 then write('x')
else write('x^',i);
end;
writeln;
close(input);
close(output);
end.
问题转述:
给出一个一元多项式各项的次数和系数,按照规定的格式要求输出该多项式。
分析:
普及组的水题。多项式大家应该很熟悉,输出的时候注意一下几点即可:
1. 最高次项为正的话开头无加号。
2. 系数为0不输出。
3. 一次项输出x,并非x^1。
4. 非常数项系数为1或-1时直接输出正负号,但是常数项需要输出该数字。
其中除第三项外其它均可在样例中检查出错误,但是若没想到第三点那么就只能得到50分了。
程序:
var i,k,n:longint;
begin
assign(input,'poly.in');reset(input);
assign(output,'poly.out');rewrite(output);
readln(n);
for i:=n downto 0 do
begin
read(k);
if k=0 then continue;
if (k>0) and (i<>n) then write('+');
if i=0 then write(k)
else if (abs(k)<>1) then write(k) else if k=-1 then write('-');
if i<>0 then
if i=1 then write('x')
else write('x^',i);
end;
writeln;
close(input);
close(output);
end.