PAT 甲级1002 A+B for Polynomials (25)
1002. A+B for Polynomials (25)
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 题意:合并多项式
思路:简单的水题,不过注意细节,譬如合并之后某一项的系数为0,那么这一项不作考虑,需要去除。
若合并之后多项式值为0,直接输出0就行了(题目貌似也没特别说明这个情况该输出什么,坑)。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
#include<cmath>
#include<vector>
#include<bitset>
#include<string>
using namespace std;
const int N_MAX = +;
struct poly {
int exp;
double coe;
bool operator <(const poly&b) const{
return exp < b.exp;
}
};
poly P1[N_MAX],P2[N_MAX],P[N_MAX];
int k1, k2;
int main() {
scanf("%d",&k1);
for (int i = ; i < k1;i++) {
scanf("%d%lf",&P1[i].exp,&P1[i].coe);
}
scanf("%d",&k2);
for (int i = ; i < k2;i++) {
scanf("%d%lf",&P2[i].exp,&P2[i].coe);
} sort(P1, P1 + k1);
sort(P2, P2 + k2); int t1 = , t2 = ,t=;
while (t1<k1&&t2<k2) {
double tmp=;//不为0即可
if (P1[t1].exp == P2[t2].exp) {
tmp= P1[t1].coe + P2[t2].coe;
if (tmp) {//系数不为0,则要算进去
P[t].exp = P1[t1].exp;
P[t].coe = P1[t1].coe + P2[t2].coe;
}
t1++;
t2++;
}
else if (P1[t1].exp<P2[t2].exp) {//那个指数小用哪个
P[t].exp = P1[t1].exp;
P[t].coe = P1[t1].coe;
t1++;
}
else if (P1[t1].exp>P2[t2].exp) {//那个指数小用哪个
P[t].exp = P2[t2].exp;
P[t].coe = P2[t2].coe;
t2++;
}
if(tmp)t++;
} while (t1 < k1) {
P[t].exp = P1[t1].exp;
P[t].coe = P1[t1].coe;
t1++;
t++;
}
while (t2 < k2) {
P[t].exp = P2[t2].exp;
P[t].coe = P2[t2].coe;
t2++;
t++;
} if (t) {
printf("%d ", t);
for (int i = ; i < t; i++) {
printf("%d %.1f%c", P[t - i - ].exp, P[t - i - ].coe, i + == t ? '\n' : ' ');
}
}
else printf("%d\n",t); }
PAT 甲级1002 A+B for Polynomials (25)的更多相关文章
- PAT甲级 1002 A+B for Polynomials (25)(25 分)
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...
- 【PAT】1002. A+B for Polynomials (25)
1002. A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynom ...
- PAT 甲级 1002 A+B for Polynomials (25 分)
1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...
- 甲级1002 A+B for Polynomials (25)
题目描述: This time, you are supposed to find A+B where A and B are two polynomials. Input Each input fi ...
- PAT 甲级 1002 A+B for Polynomials
https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000 This time, you are sup ...
- PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)
This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...
- PAT甲级——1002 A+B for Polynomials
PATA1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomia ...
- 【PAT甲级】1009 Product of Polynomials (25 分)
题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...
- 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 suppos ...
随机推荐
- Linux centos 6 配置php环境,扩展redis
1.首先安装一个虚拟机(我自己版本:VM 10.0.4) yum -y install openssl psmisc openssl-devel php-devel pcre-devel gcc gc ...
- lua 使用正则表达式分割字符串
function string_split(str, delimiter) if str == nil or str == '' or delimiter == nil then return ni ...
- k8s资源指标API及metrics-server资源监控
简述: 在k8s早期版本中,对资源的监控使用的是heapster的资源监控工具. 但是从 Kubernetes 1.8 开始,Kubernetes 通过 Metrics API 获取资源使用指标,例如 ...
- Thinkphp5的安装
很长没有码代码了,现在开始做这件事情的意义已经完全与以前不一样了.因为最近有相当长的一段休息时间,是个学习的好时间啊.之前接触过TP3.2,听说后来的版本有挺大的改动,因此呢,现在终于有时间可以好好的 ...
- linux 下 docker-compose安装
docker和dockers-compose的版本兼容对照 以下是我的服务器的相关信息 linux版本 [root@izbp16fm097gaw3tdaog2wz bin]# cat /proc/ve ...
- zookeeper伪集群(一)
Zookeeper的安装和配置十分简单, 既可以配置成单机模式, 也可以配置成伪集群模式.集群模式. 本人将对伪集群.集群进行重点介绍: 铺垫: 1.集群必须是奇数(2N+1),伪集群和集群一致. 2 ...
- python中文件操作的基本方法
在python中对一个文件进行操作,分为三大步:打开,操作,关闭 首先创建一个文件hello,里面内容为hello world 一.打开一个文件 1.#open(‘文件名或文件路径’,‘操作模式’,文 ...
- 给vagrant中的precise64升级VBoxGuestAdditions
位置:/usr/share/virtualbox/VBoxGuestAdditions.iso 在host(ubuntu 12.04 64)中: 查看虚拟机的名字:jb@H38:~/vm/vagran ...
- 如何用好 Google 等搜索引擎?
看见知乎上如何用好Google搜索的问题(http://www.zhihu.com/question/20161362),整理一下.感谢知乎大神 1.Choose Which Google? HTTP ...
- python面试题解析(python基础篇80题)
1. 答:出于编程的喜爱,以及行业本身的前瞻性,创造性,优越性,越是综合的科目越能检验一个人的能力,喜欢这种有挑战的事情. 2. 答:跟随老师学习,以及自己查询资料,结合实战,进行输入输出以及 ...