PTA (Advanced Level) 1002 A+B for Polynomials
1002 A+B for Polynomials
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 N1 aN1 N2 aN2 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (,) are the exponents and coefficients, respectively. It is given that 1,0.
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
题目解析
本题给出两个多项式,每个多项式按照 项数K 指数N1 系数aN1 指数N2 系数aN2 …… 指数NK 系数aNK 的方式给出,要求计算两个多项式的和。
用一个double型ans来记录两个多项式的和 (下标为指数,下标对应的和为系数),每输入一组指数与系数,就将系数加入ans中对应项中去。在输入数据时应记录输入的最大指数与最小指数。
遍历ans由最小指数到最大指数,获取系数不为0的项数记为cnt,cnt便是两个多项式的和的项数。
出数cnt,由最大指数到最小指数遍历并输出每一个系数不为0的项即可。
#include <bits/stdc++.h>
using namespace std;
const int MAX= 1e5+;
double ans[MAX];
int main()
{
int n;
scanf("%d", &n); //输入多项式A的项数
int maxe = INT_MIN, mine = INT_MAX;
//maxe保存最大指数 mine保存最小指数
for(int i = ; i < n; i++){ //输入多项式a
int ea; //指数
double temp; //系数
scanf("%d", &ea);
scanf("%lf", &temp);
ans[ea] += temp; //系数加入ans中对应项
maxe = max(maxe, ea); //记录最大指数
mine = min(mine, ea); //记录最小指数
}
scanf("%d", &n); //输入多项式b
for(int i = ; i < n; i++){
int eb; //指数
double temp; //系数
scanf("%d", &eb);
scanf("%lf", &temp);
ans[eb] += temp; //系数加入ans中对应项
maxe = max(maxe, eb); //记录最大指数
mine = min(mine, eb); //记录最小指数
}
int cnt = ;
//cnt记录A+B的项数
for(int i = mine; i <= maxe; i++){ //由最小指数到最大指数遍历ans
if(ans[i] != 0.0) //若某项系数不为0则项数加1
cnt++;
}
printf("%d", cnt); //输出cnt
for(int i = maxe; i >= mine; i--){ //由最大指数到最小指数遍历并输出每一个系数不为0的项
if(ans[i] != 0.0)
printf(" %d %.1f", i, ans[i]);
}
printf("\n");
return ;
}
PTA (Advanced Level) 1002 A+B for Polynomials的更多相关文章
- PAT (Advanced Level) 1002. A+B for Polynomials (25)
为0的不要输出. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- PTA (Advanced Level) 1009 Product of Polynomials
1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA(Basic Level)-1002 写出这个数
一 1002 写出这个数 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1010 ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PTA (Advanced Level) 1008 Elevator
Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
随机推荐
- 《Effective C++》 目录:
转自:http://blog.csdn.net/KangRoger/article/details/44706403 目录 条款1:视C++为一个语言联邦 条款2:尽量以const.enum.inli ...
- Beta 冲刺 (1/7)
Beta 冲刺 (1/7) 队名:洛基小队 峻雄(组长) 已完成:β版的初步计划 后两天计划:设计角色的技能树 剩余任务:角色的技能 困难:关于技能施放以及相关伤害计算等的代码 非易 已完成:角色交互 ...
- 在学习前端的路上,立下一个Flag
今天开始百度前端学习,以此为证
- 2.Redis集群环境搭建
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 一.基本概念 1.redis集群是一个可以在多个节点之间进行数据共享的设施.redis集群提供了以下两个好处1 ...
- jQuery 动态加载下拉框选项(Django)
function change_style() { $.ajax({ url: "{% url 'change_style' %}", type: "GET", ...
- text/html & text/plain的区别
需要了解的概念 Content-Type:用于定义用户的浏览器或相关设备如何显示将要加载的数据,或者如何处理将要加载的数据 MIME:MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型 ...
- Python各类并发模版
以后在写一些Python并发的时候参考下面这个模块,小西总结的挺全的,直接搬砖了. 进程并发 from multiprocessing import Pool, Manager def func(d, ...
- shiro实战系列(十)之Subject
毫无疑问,在 Apache Shiro 中最重要的概念就是 Subject.'Subject'仅仅是一个安全术语,是指应用程序用户的特定 安全的“视图”.一个 Shiro Subject 实例代表了一 ...
- Leetcode——64. 最小路径和
题目描述:题目链接 同样对于这个问题,我们可以考虑用动态规划来解决. 解决动态规划常见的三个步骤: 1:问题的归纳.对于 i,j 位置上的最短路径可以用d[ i ][ j ]表示. 2:归纳递推式:d ...
- Android Exception Type "share_dialog_title" is not translated in en, zh-rTW strings
异常出现的场景:打包Android项目时出现 解决办法: Eclipse > Preference > Android > Lint Error Checking搜索Messages ...