Pat 1002

//求两个多项式的和,
//输出多项式的项数,系数不为零的项的幂和系数
//多项式为0时只输出一个0

//说一下自己的方法
//先将多项式按幂排序(递减)
//然后类似合并排序的方法求和
//

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
map<string,double>var;
const int maxn=10000;
class node {
public:
    int a;
    double b;
    void get_ab() {
        cin>>a>>b;
    }

    node add_b_to_a(node _b) {
        node _ans;
        _ans.b=this->b+_b.b;
        _ans.a=this->a;
        return _ans;
    }

    void show() {
        printf("%d %.1lf ",this->a,this->b);
    }
    /*  bool operator <( node x) {
          if(this->a<x.a)
              return true;
          return false;
      }*/
};
bool cmp( node _first ,node _second) {
    if(_first.a>_second.a)
        return true;
    return false;
}

node list1[maxn],list2[maxn],list3[maxn];

int main() {
    int k1,k2,i,cnt,k;
    while(cin>>k1) {
        for(i=0; i<k1; i++) {
            list1[i].get_ab();
        }
        sort(list1,list1+k1,cmp);
        cin>>k2;
        for( i=0; i<k2; i++) {
            list2[i].get_ab();
        }
        sort(list2,list2+k2,cmp);

        for(cnt=0,i=0,k=0; i<k1&&k<k2 ; cnt++) {
            if(list1[i].a>list2[k].a) {
                list3[cnt]=list1[i];
                i++;
                continue;
            }
            if(list1[i].a<list2[k].a) {
                list3[cnt]=list2[k];
                k++;
                continue;
            }
            if(list1[i].a==list2[k].a) {
                if(list1[i].b+list2[k].b!=0)
                    list3[cnt]=list1[i].add_b_to_a(list2[k]);
                else cnt--;
                k++;
                i++;

                continue;
            }
        }
        for(; i<k1; i++)
            list3[cnt++]=list1[i];
        for(; k<k2; k++)
            list3[cnt++]=list2[k];
        if(cnt!=0)
        { printf("%d ",cnt);
            for(i=0; i<cnt-1; i++)
                printf("%d %.1f ",list3[i].a,list3[i].b);
            printf("%d %.1f\n",list3[i].a,list3[i].b);
        }
        else printf("0\n");
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值