PTA甲级 1002 A+B for Polynomials (25 分)

题目链接

https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000

题目大意

给定AB两个多项式(各一行)
2 1 2.4 0 3.2
2 2 1.5 1 0.5
格式如下
多项式
其中K是多项式中非零项的个数,N分别是指数和系数。
输出格式也按照上方。

解题思路

map暴力。
需要注意的地方是,当多项式系数为0时,指数和系数都不需要输出,相应的;当多项式整体为0时,只需要输出0。有亿点坑。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,double> PID;
#define mp make_pair

int main(){
    map<int,double> m;
    int n;
    cin >> n;
    for(int i = 0;i < n;i++){
        int x; 
        double y;
        cin >> x >> y;
        if(m.count(x) == -1){
            m[x] = y;
        }else{
            m[x] += y;
        }
    }
    cin >> n;
    for(int i = 0;i < n;i++){
        int x;
        double y;
        cin >> x >> y;
        if(m.count(x) == -1){
            m[x] = y;
        }else{
            m[x] += y;
        }
    }
    stack<PID> st;
    int all = 0;
    for(auto it : m){
    	//cout << it.first << "->"<< it.second << "\n";
    	if(it.second != 0){
    		all++;
    		st.push(mp(it.first,it.second));
		}
	}
	if(all == 0){
		cout << all;
	}else{
		cout << all;
		while(!st.empty()){
			cout << " "<< st.top().first << " ";
			printf("%.1f",st.top().second);
			st.pop(); 
		}
	}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值