PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642

题目描述:

This time, you are supposed to find A+B where A and B are two polynomials.

译:这一次,你要找到 A + B ,其中 A , B 是两个多项式


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 (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1 ≤ K ≤ 100 ≤ NK < ⋯ < N2 < N1 ≤ 1000.

译:每个输入文件包含一个测试用例,每个用例占两行,每行包含多项式的如下信息 : K N1 aN1 N2 aN2 ... NK aNKk是多项式中非零项的个数,NiaNi (i=1,2,⋯,K) 分别表示多项式的 指数 和 系数。 已知 1 ≤ K ≤ 100 ≤ NK < ⋯ < N2 < N1 ≤ 1000.


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.

译:对于每个测试用例,你应该在一行中输出 A + B 的和,和输入具有相同的格式。注意:在行末不能有多与的空格,并且精确到1位小数。


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

The Idea:

/*
(1) 对于多项式的存储,由于每一个指数 int 型对应有一个系数 double 型 ,故利用 map ,
由于输出的时候需要按照系数从大到小的输出,所以利用 按照 key 的值降序排列的 map 结构。
*/
map<int , double , greater<int> > mp; /*
(2) 但是值得注意的是:题目中说明是非零项的个数,那么很有可能在相加的时候出现系数为 0 的时候
这种时候显然这一项已经不符合我们的要求。
为此我再设立了一个 map 用来存储满足要求的答案
*/

The Codes:

#include<bits/stdc++.h>
using namespace std ;
map<int , double , greater<int> > mp ;// 储存最初 A + B 的和 ,默认排序方式为 key 的降序
map<int , double , greater<int> > ans ; // 储存本题 A + B 的和 ,默认排序方式为 key 的降序
int main(){
int k1 , k2 , n ;
double an ;
cin >> k1 ; // 输入 A 的非零项数 以及每一项的 指数 和 系数
for(int i = 0 ; i < k1 ; i ++){
cin >> n >> an ;
mp[n] += an ; // 以指数为 key ,输入的 值 增加到 value 上
}
cin >> k2 ; // 输入 B 的非零项数 以及每一项的 指数 和 系数
for(int i = 0 ; i < k2 ; i ++){
cin >> n >> an ;
mp[n] += an ; // 以指数为 key ,输入的 值 增加到 value 上
}
for(map<int,double,greater<int> >::iterator it =mp.begin() ; it != mp.end() ;it ++)
if(fabs((*it).second ) > 1e-6) // 浮点数,判断是否 系数相加之后 为 0
ans[(*it).first] = (*it).second ;// 非零项用 ans 存储
cout << ans.size() ; // 输出 A + B 的和 非零项数
for(map<int,double,greater<int> >::iterator it =ans.begin();it != ans.end();it++){
//cout<<" "<<(*it).first<<" "<<(*it).second;
printf(" %d %.1f",(*it).first,(*it).second);
}
cout << endl ; // 行尾换行符
return 0;
}

PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642的更多相关文章

  1. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...

  2. PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...

  3. PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...

  4. PAT (Advanced Level) Practice 1002 A+B for Polynomials 分数 25

    This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...

  5. PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  6. PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...

  7. PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...

  8. PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642 题目描述: At the beginning of ever ...

  9. PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 目录 PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) ...

随机推荐

  1. PerformanceObserver API All In One

    PerformanceObserver API All In One 性能监控 https://developer.mozilla.org/en-US/docs/Web/API/Performance ...

  2. How to using PyPI publish a Python package

    How to using PyPI publish a Python package PyPI & Python package https://pypi.org/ main make a f ...

  3. brew & apply2files bug

    brew & apply2files bug Error: Permission denied @ apply2files - /usr/local/lib/node_modules/npm/ ...

  4. vscode & typescript & optional-chaining bug

    vscode & typescript & optional-chaining bug https://www.cnblogs.com/xgqfrms/p/11745541.html ...

  5. vueJS+ES6开发移动端APP实战项目笔记

    一.什么是MVVM框架 MV*包括MVC.MVP.MVVM MVVM框架由Model.View.ViewModel构成. Model指的是数据,在前端对应的是JavaScript对象. View指的是 ...

  6. 初学c++,vc++6.0必备!

    文章首发 | 公众号:lunvey 作为一个纯粹的萌新,工作需要,刚接触到c++. 按照以往的经验,配置一个开发环境是首要的,其次便是边学边敲. c++入门书籍寻找了一堆,发现了一个共同点,在Wind ...

  7. 关于VsCode创建Vue基础项目的步骤以及相关问题

    项目创建步骤: 1.  全局安装vue-cli 安装命令: npm install -g vue-cli 在vscode上打开一个终端,然后输入此命令(下为截图): 2. 安装webpack,打包js ...

  8. 653. 两数之和 IV - 输入 BST + HashSet

    653. 两数之和 IV - 输入 BST 题目描述 题解分析 最简单的方法就是遍历整棵树,找出所有可能的组合,判断是否存在和为 kk 的一对节点.现在在此基础上做一些改进. 如果存在两个元素之和为 ...

  9. PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断

    Travelling Salesman Problem PAT-1150 #include<iostream> #include<cstring> #include<st ...

  10. AI换脸

    AI换脸 技术 调用到百度的AI接口,layui的图片上传,栅格化布局 核心代码 纯py文件运行 # encoding:utf-8 import requests import base64 impo ...