PAT甲级——A1002 A+B for Polynomials
2024-10-07 23:20:49
This time, you are supposed to find A+B where A and B are two polynomials.
Input Specification:
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 (,) are the exponents and coefficients, respectively. It is given that 1,0.
Output Specification:
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
使用map进行映射即可,但需注意输出格式
#include <iostream>
//#include <vector>
#include <map>
#include <stack>
using namespace std; int main()
{
map<double, double,greater<double>>data;//从大到小排序
for (int i = ; i < ; ++i)//输入两组数据
{
int k;
cin >> k;
for (int j = ; j < k; ++j)//接受每组数据
{
double a, b;
cin >> a >> b;
data[a] += b;
if (data[a] == )//系数为0则删除
data.erase(a);
}
}
cout << data.size();
for (auto ptr = data.begin(); ptr != data.end(); ++ptr)
{
cout << " " << ptr->first << " ";
printf("%.1f", ptr->second);
}
return ;
}
最新文章
- install usb serial
- POJ 3130 How I Mathematician Wonder What You Are!(半平面交求多边形的核)
- cocos2dx Android 环境搭建 以及 ndk调试
- php开启curl和openssl
- Javascript获取某个月的天数-简单方法 .(转别人的)
- AFNetworking2.0后 进行Post请求
- DocFX生成PDF文档
- Talk 3: Rob Pike on Upspin (Gopherfest 2017)
- java.lang.Boolean 类源码解析
- DS哈希查找--线性探测再散列
- 【转】每天一个linux命令(49):at命令
- 【转】SSL协议详解
- 尺取法拓展——POJ3320
- 浅谈splay
- CryptoZombies学习笔记——Lesson4
- Mybatis 一个搜索框对多个字段进行模糊查询
- chrome浏览器自动填充失效问题
- windows安装redis并设置别名
- 飞镖(bzoj 2335)
- 第七篇 elasticsearch 链接mysql不会更新