题目

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. [Asp.Net Core] Blazor Server Side 项目实践 - 切换页面时保留状态

    前言: 这是 项目实践系列 , 算是中高级系列博文, 用于为项目开发过程中不好解决的问题提出解决方案的. 不属于入门级系列. 解释起来也比较跳跃, 只讲重点. 因为有网友的项目需求, 所以提前把这些解 ...

  2. 杂园日记-H5-IOS-Android混合开发

    1.js 调用 原生API iOS: window.webkit.messageHandlers.yourFunName.postMessage({"1":"3" ...

  3. IIS WebDAV安全配置

    本文为转载,原文地址:http://www.2cto.com/article/201307/228165.html IIS WebDAV安全配置 2013-07-16 12:13:00 作者:瞌睡龙收 ...

  4. phpstudy xdebug 配置

    来源:https://baijiahao.baidu.com/s?id=1607680791440431678&wfr=spider&for=pc https://www.cnblog ...

  5. (第五篇)Linux操作系统基本结构介绍

    Linux操作系统基本结构介绍 Linux系统一般有4个主要部分:内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用 ...

  6. POJ1475 推箱子---模块化思想

    自古逢秋悲寂寥,我言秋日胜春朝. 晴空一鹤排云上,便引诗情到碧霄. --刘禹锡 题目:推箱子 网址:http://poj.org/problem?id=1475 推箱子游戏相信大家都不陌生,在本题中, ...

  7. python 类C数组的两种形式:list -->内容可变, tuple --->内容不可变

    python 中的列表相当与 C 中的数组,列表:list 初始化使用[ ], 元组:tuple 初始化使用(): 一.列表list 1 #!/usr/bin/python  2   3 #list初 ...

  8. Docker 1.13.0 详细更新日志

    本文讲的是Docker 1.13.0 详细更新日志[编者的话]Docker发布1.13.0版本,此次版本更新内容较多,以下为该版本具体的变更. 构建 添加在构建时可以指定镜像用作高速缓存源的功能. 这 ...

  9. 10 微信小程序路由跳转

    一.四种跳转方式 API路由详解 除了tabBar这种底部跳转的方法,我们还有路由跳转,以下四种方式: 1. wx.switchTab() :跳转到 tabBar 页面,并关闭其他所有非 tabBar ...

  10. 25-Java-Spring框架(三)

    Spring框架的了解.SpringIOC的部分内容请阅读23-Java-Spring框架(一) SpringwebMVC的了解.请求流程.运用等请阅读24-Java-Spring框架(二) 四.Sp ...