pat1002. A+B for Polynomials (25)

嗯也是个简单题

#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
struct Node{
    int coeff=0;
    double expon;
};
int main(){
    ios::sync_with_stdio(false);
    stack<Node>a;
    stack<Node>b;
    stack<Node> res;
    Node tmp;
    int m,n;
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>tmp.coeff>>tmp.expon;
        a.push(tmp);
    }
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>tmp.coeff>>tmp.expon;
        b.push(tmp);
    }
    Node ta,tb;
    while(a.size() && b.size()){
        ta = a.top();
        tb = b.top();
        if(ta.coeff==tb.coeff){
            tmp.coeff = ta.coeff;
            tmp.expon = ta.expon+tb.expon;
            if(tmp.expon) res.push(tmp);
            a.pop();
            b.pop();
        }else{
            if(ta.coeff>tb.coeff){
                tmp.coeff = tb.coeff;
                tmp.expon = tb.expon;
                res.push(tmp);
                b.pop();
            }
            if(ta.coeff<tb.coeff){
                tmp.coeff = ta.coeff;
                tmp.expon = ta.expon;
                res.push(tmp);
                a.pop();
            }
        }
    }
    while(a.size()){
        ta = a.top();
        tmp.coeff = ta.coeff;
        tmp.expon = ta.expon;
        a.pop();
        res.push(tmp);
    }
    while(b.size()){
        tb = b.top();
        tmp.coeff = tb.coeff;
        tmp.expon = tb.expon;
        res.push(tmp);
        b.pop();
    }
    cout<<res.size();
    while(res.size()){
        tmp = res.top();
        res.pop();
    //  cout<<" "<<tmp.coeff<<" "<<tmp.expon;
        printf(" %d %.1f",tmp.coeff,tmp.expon);
    }
    return 0;
}

这个题要注意的是答案中指数只可以有一个小数
还有就是要注意当两个数的指数相加为0的时候的处理

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值