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 ...
随机推荐
- python之函数默认参数的坑
坑 当你的默认参数如果是可变的数据类型,你要小心了 例题 # 正常没毛病的操作 def func(a,b=False): print(a) print(b) func(1,True) # 在实参角度, ...
- JAVA - Annotation 注解 入门
Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...
- java基础—泛型
一.体验泛型 JDK1.5之前的集合类中存在的问题——可以往集合中加入任意类型的对象,例如下面代码: 1 package cn.gacl.generic.summary; 2 3 import jav ...
- PayPal为什么从Java迁移到Node.js 性能提高一倍 文件代码减少44%
大家都知道PayPal是另一家迁移到Node.js平台的大型公司,Jeff Harrell的这篇博文 Node.js at PayPal 解释了为什么从Java迁移出来的原因: 开发效率提高一倍(2 ...
- JavaScript数组之傻傻分不清系列(split,splice,slice)
因业务场景需求,需要将一个数组截断而不需要影响原数组.这里来理解一下 slice,splice,split slice() 从某个已有的数组返回选定的元素.(JavaScript Array 对象) ...
- [BZOJ] 1563: [NOI2009]诗人小G
1D/1D的方程,代价函数是一个p次函数,典型的决策单调性 用单调队列(其实算单调栈)维护决策点,优化转移 复杂度\(O(nlogn)\) #include<iostream> #incl ...
- 15Shell脚本—流程控制
流程控制语句 尽管可以通过使用Linux命令.管道符.重定向以及条件测试语句编写最基本的Shell脚本,但是这种脚本并不适用于生产环境.原因是它不能根据真实的工作需求来调整具体的执行命令,也不能根据某 ...
- destoon ip接口失效修改 修改后偶尔会加载很慢
因为百度ip转换增加了密匙验证,所以导致之前的接口无法再转换ip地址的信息,修复方法如下:打开include\cloud.func.php文件搜索: function iplookup($ip) { ...
- LeetCode(162) Find Peak Element
题目 A peak element is an element that is greater than its neighbors. Given an input array where num[i ...
- CentOS 7 配置OpenCL环境(安装NVIDIA cuda sdk、Cmake、Eclipse CDT)
序 最近需要在Linux下进行一个OpenCL开发的项目,现将开发环境的配置过程记录如下,方便查阅. 完整的环境配置需要以下几个部分: 安装一个OpenCL实现,基于硬件,选择NVIDIA CUDA ...