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 ...
随机推荐
- sql注入--基础
什么是sql注入: 利用SQL语句 在外部 对数据库进行 查询,更新等 动作 sql注入的原理: 输入值可控且带入数据库执行(前提) 接受的变量传值未进行过滤(实质) sql注入的目的: 获取数据(网 ...
- Spring Boot 验证表单
在实际工作中,得到数据后的第一步就是验证数据的正确性,如果存在录入上的问题,一般会通过注解校验,发现错误后返回给用户,但是对于逻辑上的错误,很难使用注解方式进行验证了,这个使用可以使用Spring所提 ...
- AlloyDesigner 使用
前端开发视觉是很重要的一部分,所以视觉的还原度很重要,今天给大家介绍一个很好用的视觉精度调整插件 一.下载AlloyDesigner插件 下载插件 二.保存视觉稿为图片格式 1.psd 用ps直接保存 ...
- Vue.js实现前段评论展示
本来想着给这个博客弄个回复系统(类似知乎的回复),最初的实现思路是这样的:主评论后台渲染,前台新增的评论,回复用jquery操作dom放到页面上.实现的时候感觉好复杂,大量的dom操作,目前前段框架不 ...
- ActivityCapture
Index: ActivityCapture.java =================================================================== --- ...
- Kubernetes1.91(K8s)安装部署过程(一)--证书安装
安装前忠告:如果你用的是虚拟机,强烈不建议你使用克隆(链接克隆)的方式,至于完整克隆不知道有没有问题,每一台全新安装centos7系统最好. 一.安装前主题环境准备 1.docker安装 建议使用官网 ...
- Mysql千万级数据删除实操-企业案例
某天,在生产环节中,发现一个定时任务表,由于每次服务区查询这个表就会造成慢查询,给mysql服务器带来不少压力,经过分析,该表中绝对部分数据是垃圾数据 需要删除,约1050万行,由于缺乏处理大数据的额 ...
- C++之友元函数和友元类
通过friend关键字,我们可以将不属于当前类的一个函数在当前类中加以声明,该函数便可以成为当前类的友元函数. #include<iostream>using namespace std; ...
- bash下输入命令的几个常用快捷键
------------------------------------------ 先区分下vi里的命令 快速在行里移动光标 b 是往前部一个单词一个单词的移动 e 是往后部一个单词一个单词的移 ...
- 【服务器】Https服务配置
1)利用openssl生成证书 2)再次修改nginx配置文件nginx.conf中的server配置 ① 是默认监听http请求的8080端口的 server (再次修改,第一次是在 用ngi ...