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 ...
随机推荐
- mac层到ath9k层,ath9k层到硬件层
如上图,整个 mac 层分成两个部分——UMAC 和 LMAC.LMAC 分成 MAC 下半部分和硬件抽象层. 硬件抽象层和ath9k层的连接 在hw.h中的函数struct ath_hw_ops() ...
- Nuget使用备忘
菜单:工具-库程序包管理器-管理解决方案的NuGet程序包,搜索,下载,安装 或者 工具-库程序包管理器-程序包管理器控制台,输入PM命令,如: install-package log4net 如果不 ...
- oracle中的树状查询
oracle中的树状查询 工作中经常会遇到将数据库中的数据以树的形式展现的需求.以下我们来看一下该需求在Oracle中如何实现. 首先我们需要有一个树形的表结构(当然有时候会出现表结构不是典型的树形结 ...
- Spring中c3p0连接池 jar包下载 c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包
c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包 下载地址: https://pan.baidu.com/s/1jHDiR7g 密码 tyek
- poj3335 Rotating Scoreboard
题目描述: vjudge POJ 题解: 半平面交判核的存在性. 重点在于一个点的核也算核. 这样的话普通的求多边形的版本就要加一个特判. 就是把剩下的一个节点暴力带回所有直线重判,这时判叉积是否$\ ...
- Unity基础-编辑器
编辑器 Special Folders Hidden Folder(start with .) Standard Assets:第一批加载的文件 Editor:只在编辑下才能使用, Plugins R ...
- jQuery发送ajax请求实现跨域访问
Java代码的话,在返回响应之前调用如下代码中的allowCrossDomainAccess()方法: /** * 允许跨域访问 */ public void allowCrossDomainAcce ...
- MySQL创建根据经纬度计算距离的函数
按照经纬度计算距离 日常开发中,特别是做微信项目时,经常会遇到根据用户地理位置来展示附近商家的功能,通常解决这种问题的思路是,后台设置商家的经纬度,然后再根据前台传的经纬度进行计算,具体经纬度转换以及 ...
- CodeForces Round #320 Div2
A. Raising Bacteria 计算一下x的bitcount就是答案. #include <iostream> #include <cstdio> #include & ...
- 关于mysql中select出数据未排序问题
来自 mysql必知必会