题目

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N​1​​ a​N​1​​ ​​ N​2​​ a​N​2​​ ​​ ... N​K​​ a​N​K​​ ​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​ ​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N

​K​​ <⋯<N​2​​ <N​1​​ ≤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.

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

题目解读

给出两个多项式,每个输入格式是 非零项个数 指数1 系数1 指数2 系数2

让计算两多项式的和,并按照指定格式输出 非零项个数 指数1 系数1 指数2 系数2

要求顺序是指数从高到低

思路解析

  • 可以用一个结构体来保存每一项的指数和系数,然后在第二次输入时根据去找到相应的那一项,对其系数进行修改。
  • 这样做既浪费存储空间也浪费时间,但一般都能想到,更好的做法是,用一个数组来取代整个结构体,每一项的指数作为数组的索引系数作为值,这样在读入时,直接找到对应位置进行修改,对数组的访问是很快的。
  • 之后一次遍历,统计出数组不为0的个数,就是非零项的个数;然后对数组从后往前输出每个非零项对应的下标和值,就是结果。

代码

#include <iostream>
using namespace std; int main() {
// 指数作为下标,系数作为值,题目给出指数最多为1000
float pols[1001] = {0};
int k, exp;
float coe;
int cnt = 0;
// 每一个样例是两行
for (int i = 0; i < 2; ++i) {
// 第一个整数是说明后面有几个非0项
cin >> k;
for (; k > 0; --k) {
// 指数 系数 指数 系数
cin >> exp >> coe;
pols[exp] += coe;
}
}
// 统计非零项
for (int i = 0; i < 1001; ++i) {
if (pols[i] != 0) cnt++;
}
// 输出非零项个数
cout << cnt;
// 按 指数 系数,从高到底输出,空格分隔
for (int i = 1000; i >= 0; --i) {
if (pols[i] != 0) {
printf(" %d %.1f", i, pols[i]);
}
} return 0;
}

PAT 1002 A+B for Polynomials (25分)的更多相关文章

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

  2. PAT 1002. A+B for Polynomials (25) 简单模拟

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

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

  4. 【PAT甲级】1002 A+B for Polynomials (25 分)

    题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...

  5. PAT 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 con ...

  6. PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

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

  8. 1002 A+B for Polynomials (25分) 格式错误

    算法笔记上能踩的坑都踩了. #include<iostream> using namespace std; float a[1001];//至少1000个位置 int main(){ in ...

  9. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

随机推荐

  1. Dockerfile的简单人门编写之关于yum的问题

    首先我们编写一个简单的Dockerfile的例子.不过再此之前大家得去把编写dockerfile的指令了解一下. 编写以 centos镜像为基础镜像,构建 http 服务,Dockerfile 要求删 ...

  2. AJAX教程——检视阅读

    AJAX教程--检视阅读 参考 AJAX 教程--菜鸟 AJAX 教程--w3cschool AJAX 教程--w3school.cn AJAX 教程--易百 AJAX = Asynchronous ...

  3. pytorch 手写数字识别项目 增量式训练

    dataset.py ''' 准备数据集 ''' import torch from torch.utils.data import DataLoader from torchvision.datas ...

  4. c++使用cin、cout与c中使用scanf、printf进行输入输出的效率问题

    在c++中,我们使用cin和cout进行输入输出会比用scanf和printf更加简洁和方便,但是当程序有大量IO的时候,使用cin和cout进行输入输出会比用scanf和printf更加耗时, 在数 ...

  5. Auth认证中的think_auth_rule type字段干嘛用的?

    昨晚认真研究了一下这个类,设计的很巧妙,但是你说的这个字段,我认为应该是作者想加功能但还没写,在session判断的地方可以看到,type这个字段实际是对应的 1-实时验证,2登陆验证 ,显然,这个字 ...

  6. Task Scheduler Error Message: 80041318

    Using the error lookup tool that comes with VC++ (errlook.exe, or "Error Lookup" on the To ...

  7. ansible的模块使用

    转载于   https://www.cnblogs.com/franknihao/p/8631302.html [Ansible 模块] 就如python库一样,ansible的模块也分成了基本模块和 ...

  8. Scala教程之:静态类型

    文章目录 泛类型 型变 协变 逆变 不变 类型上界 类型下界 内部类 抽象类型 复合类型 自类型 隐式参数 隐式转换 多态方法 类型推断 Scala是静态类型的,它拥有一个强大的类型系统,静态地强制以 ...

  9. 汇编 之 win10 下安装dosbox 和 MASM

    所需工具链接: 链接:https://pan.baidu.com/s/1nenMsSdgEkeRKc6wh9DQRA 提取码:1r89 只需要以下两个工具 安装dosbox 和MASM步骤 (1)解压 ...

  10. 计算5的n次幂html代码

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