多项式相加

【培训试题】多项式相加(normal)

 

Time Limit:1000MS  Memory Limit:65536K
Total Submit:1109 Accepted:212

Description

求两个一元多项式的和。输入多项式方式为,多项式项数,每项系数;如求多项式2x2+3x-4和多项式2x+2的和。

Input

第一行,第一个多项式的系数(最多为200),指数(最多为200) 
第二行,第二个多项式的系数(最多为200),指数(最多为200) 

Output

两个多项式的和

Sample Input

2 2 3 1 -4 0
2 1 2 0

Sample Output

2x^2+5x-2

Hint

注意输出格式




【解析】
是一个很简单的模拟,只需统计各次项的系数再按要求输出即可。但是这道题可以用来锻炼自己的编程能力和细心程度。Hint 专门提示了注意输出格式,但我还是没有一次AC。下面是几条需要注意的地方。
  • 从高次向低次依次输出。
  • 第一项若为正,不能输出'+'号,否则必须输出。
  • 对于除第一项的任意一项,若系数为1,则只输出'+';对不除第一项的任意一项,若系数为-1,则只输出'-'。
  • 对于次数为1的项,不能输出'^1'。
  • 最后一定要换行。
程序依旧写丑了,但是看起来比较明白,KISS原则。

var
        m:array[0..200] of longint;
        i,j,a,b:longint;

begin
        //assign(input,'1006.in');reset(input);
        while not eoln do
                begin
                        read(a,b);
                        inc(m[b],a);
                end;
        readln;
        while not eoln do
                begin
                        read(a,b);
                        inc(m[b],a);
                end;
        for i:=200 downto 0 do
                begin
                        if m[i]<>0 then
                                begin
                                        j:=i;
                                        break;
                                end;
                end;
        if j<>0 then
                begin
                        if j>1 then
                                begin
                                        if m[j]>0 then
                                                begin
                                                        if m[j]>1 then
                                                                write(m[j],'x^',j)
                                                        else
                                                                write('x^',j);
                                                end
                                        else
                                                begin
                                                        if m[j]<-1 then
                                                                write(m[j],'x^',j)
                                                        else
                                                                write('-x^',j);
                                                end;
                                end
                        else
                                begin
                                        if (m[j]>1) or (m[j]<-1) then
                                                write(m[j],'x')
                                        else
                                                begin
                                                        if m[j]=-1 then
                                                                write('-x')
                                                        else
                                                                write('x');
                                                end;
                                end;
                end
        else
                begin
                        writeln(m[j]);
                        halt;
                end;
        for i:=j-1 downto 0 do
                if m[i]<>0 then
                        begin
                                if i=0 then
                                        begin
                                                if m[i]>0 then
                                                        writeln('+',m[i])
                                                else
                                                        writeln(m[i]);
                                                halt;
                                        end;
                                if i=1 then
                                        begin
                                                if m[i]>0 then
                                                        begin
                                                                if m[i]>1 then
                                                                        write('+',m[i],'x')
                                                                else
                                                                        write('+x');
                                                        end
                                                else
                                                        begin
                                                                if m[i]<-1 then
                                                                        write(m[i],'x')
                                                                else
                                                                        write('-x');
                                                        end;
                                                continue;
                                        end;
                                if m[i]>0 then
                                        begin
                                                if m[i]>1 then
                                                        write('+',m[i],'x^',i)
                                                else
                                                        write('+x^',i);
                                        end
                                else
                                        begin
                                                if m[i]<-1 then
                                                        write(m[i],'x^',i)
                                                else
                                                        write('-x^',i);
                                        end;
                        end;
        writeln;
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值