PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642
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 ≤ 10,0 ≤ NK < ⋯ < N2 < N1 ≤ 1000.
译:每个输入文件包含一个测试用例,每个用例占两行,每行包含多项式的如下信息 : K N1 aN1 N2 aN2 ... NK aNK ,k是多项式中非零项的个数,Ni 和 aNi (i=1,2,⋯,K) 分别表示多项式的 指数 和 系数。 已知 1 ≤ K ≤ 10,0 ≤ 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的更多相关文章
- 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) ...
- 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 ...
- PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...
- 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 ...
- 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 ...
- PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...
- PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...
- 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 ...
- PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 目录 PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) ...
随机推荐
- IM & WebSockets
IM & WebSockets WebSocket API https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API ht ...
- TS & error
TS & error Function implementation is missing or not immediately following the declaration.ts ht ...
- NGK Global英国路演落下帷幕,区块链赋能大数据取得新突破
NGK全球巡回路演于7月25日在英国圆满举行,此次路演是由NGK英国社区主办,旨在探讨当前大数据爆炸的形式下,区块链如何赋能,解决行业痛点.会上,行业精英.区块链爱好者.各实体产业代表以及科技人员纷纷 ...
- sublime 使用过程中遇到的问题
1.当我把鼠标放置在下图所示的class上几秒钟后,sublime就会在全局查找当前的class字符,这时sublime就会出现卡顿或无响应 解决方法: 点击preferences下的settings ...
- Filter理解
web中Filter通过<init-param>添加参数.web.xml中的配置: <filter> <filter-name>AuthFilter</fil ...
- MySQL如何搭建主库从库(Docker)
目录 MySQL主从搭建 一.主从配置原理 二.操作步骤 1.创建主库和从库容器 2.启动主从库容器 3.远程连接并操作主从库 4.测试主从同步 MySQL主从搭建 一.主从配置原理 mysql主从配 ...
- 182. 查找重复的电子邮箱 + group by + having
182. 查找重复的电子邮箱 LeetCode_MySql_182 题目描述 方法一:使用笛卡尔积 # Write your MySQL query statement below select di ...
- ThinVnc-身份验证绕过(CVE-2019-17662)
ThinVnc-身份验证绕过(CVE-2019-17662) 简介 ThinVNC是一款以网页浏览器为基础设计的远端桌面连接工具,不局限用户端使用那种作业平台,都可以通过采用HTML5为标准的浏览器来 ...
- SpringCloud里面切换数据源无效的问题
问题描述: 调用链:controller1的接口A->service1的方法A->service2的方法B 方法A开启了事务,且指定了数据库A的数据源 方法B也开启了事务,使用了默认的事务 ...
- FreeBSD 12.2 vmware 虚拟机镜像 bt 种子
安装了 KDE5 火狐浏览器 Fcitx 输入法 并进行了中文设置 替换软件源为国内可用. VirtualBox虚拟机也可以用 magnet:?xt=urn:btih:E88885631B57426 ...