1002. A+B for Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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)的更多相关文章

  1. 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 ...

  2. 【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 ...

  3. 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 ...

  4. 甲级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 ...

  5. PAT 甲级 1002 A+B for Polynomials

    https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000 This time, you are sup ...

  6. 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 ...

  7. 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 ...

  8. 【PAT甲级】1009 Product of Polynomials (25 分)

    题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...

  9. 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 ...

随机推荐

  1. 获取页面的title值

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 标签input的value属性和placeholderde 区别

    placeholder 顾名思义是一个占位符 在你的value为空的时候他才会显示出来,但是他本身并不是value,也不会被表单提交.

  3. ssh整合思想 Spring分模块开发 crud参数传递 解决HTTP Status 500 - Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or(增加事务)

    在Spring核心配置文件中没有增加事务方法,导致以上问题 Action类UserAction package com.swift.action; import com.opensymphony.xw ...

  4. [转载]matlab图像处理为什么要归一化和如何归一化

    matlab图像处理为什么要归一化和如何归一化,一.为什么归一化1.   基本上归一化思想是利用图像的不变矩寻找一组参数使其能够消除其他变换函数对图像变换的影响.也就是转换成唯一的标准形式以抵抗仿射变 ...

  5. Keras预训练模型下载后保存路径

    https://blog.csdn.net/xiaohuihui1994/article/details/83340080

  6. NowCoder 9.9 模拟赛

    T1.中位数 二分答案x,原序列大于x的置为1,小于x的置为-1,判断是否存在长度大于m的区间和大于0(也就是大于x的数多于小于x的数),若有,则ans>=x,否则ans<x #inclu ...

  7. python入门:求1-2+3-4+5...99的所有数的和(自写)

    #!/usr/bin/env pyhton # -*- coding:utf-8 -*- #求1-2+3-4+5...99的所有数的和(自写) """ 给x赋值为0,给y ...

  8. destoon 自定义session丢失

    destoon 在使用session之前 应该实例化 $s​ession = new dsession(); destoon通过配置文件加载了不同session存储方式.如果你使用session的页面 ...

  9. 《linux设备驱动开发详解》笔记——8阻塞与非阻塞IO

    8.1 阻塞与非阻塞IO 8.1.0 概述 阻塞:访问设备时,若不能获取资源,则进程挂起,进入睡眠状态:也就是进入等待队列 非阻塞:不能获取资源时,不睡眠,要么退出.要么一直查询:直接退出且无资源时, ...

  10. Python之简单Socket编程

    Socket编程这块儿还是比较重要的,记录一下:实现服务器端和客户端通信(客户端发送系统指令,如ipconfig等,服务器端执行该指令,然后将指令返回结果给客户端再传过去,设置一次最多直接收1024字 ...