PAT 甲级练习 1002
来源:互联网 发布:淘宝优惠券 文胸 编辑:程序博客网 时间:2024/05/19 05:39
1002 A+B for Polynomials
This time, you are supposed to find A+B where A and B are two polynomials.
Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < … < N2 < N1 <=1000.
Output
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 2 1.5 1 2.9 0 3.2
#include <iostream>#include <map>#include <iomanip>using namespace std;int main() { bool insertion; // find the insertion point int K1, K2, expo; double coef; map<int, double> result; cin >> K1; for(int i=0; i<K1; ++i){ cin >> expo >> coef; result[expo] = coef; } cin >> K2; for(int i=0; i<K2; ++i){ cin >> expo >> coef; if(result.find(expo) == result.end()) // not found result[expo] = coef; else // found if(result[expo] + expo == 0) result.erase(result.find(expo)); else result[expo] += expo; } if(result.empty()) cout << 0 << endl; // no element else{ // element exist cout << result.size() << ' '; int i = 0, lmt = result.size(); for(auto it=result.rbegin(); it!=result.rend(); ++it){ cout << (*it).first << ' ' << setprecision(1) << fixed << (*it).second; cout << ((++i == lmt) ? '\n':' '); } } return 0;}
Pay attention that when using map.erase(arg)
, the arg must be an iterator but not a key.
- PAT 甲级练习 1002
- PAT 甲级练习 1001
- PAT甲级练习1025. PAT Ranking (25)
- PAT-甲级1002
- PAT甲级1002
- PAT甲级习题(1002)
- PAT-甲级-1002
- PAT甲级1002
- PAT甲级1001,1002
- PAT甲级 1002
- PAT甲级1002题解
- PAT甲级1002
- 浙大PAT甲级-1002
- PAT甲级 1002
- 【PAT】甲级1002
- pat甲级1002
- PAT甲级练习1003. Emergency (25)
- PAT甲级练习1004. Counting Leaves (30)
- 是时候学习python了
- 交叉编译libpng和libjpeg库
- 机器学习基石-05-1-Recap and Preview
- 2017.11.03【NOIP提高组】冲刺A组 总结
- POJ
- PAT 甲级练习 1002
- Climbing Stairs(算法分析week9)
- 关于myeclipse10与maven整合的问题
- La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017
- awk内置变量之FILENAME、ARGC、ARGV、ENVIRON
- 基于Docker和Golang搭建Web服务器
- 如何保证kafka 的消息机制
- 【NOIP 2016 day1 T1 T3】玩具谜题,换教室——题解
- 微信服务器接入流程