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 ...
随机推荐
- Android高级_第三方下载工具Volley
Volley下载主要应用于下载文本数据和图片数据两个方向,下面分别介绍: 一.使用Volley开启下载,首先要做的是导包和添加权限: (1)在build.gradle文件中导入依赖包:compile ...
- zabbix之运维疑难总结
2.trousers包 zabbix默认情况下要求trousers包的版本是0.3.12版本以上.如果低于这个版本,有可能zabbix-server服务启动不成功.在mysql7.1版本以上可能会使用 ...
- Mysql缓存中innodb_buffer_pool与Qcache的区别
innodb buffer pool和Qcache的缓存区别? 1.Qcacche缓存的是SQL语句及对应的结果集,缓存在内存,最简单的情况是SQL一直不重复,那Qcache的命令率肯定是0; 2.b ...
- Xmind在win10更改用户设置后出现Invalid Configuration Location 错误的解决办法
错误原因: 因为一开始新建win10用户时,使用的是中文用户名,导致了部分软件比如IDEA读取C盘中配置文件时报错.我用管理员权限修改用户姓名为英文后,IDEA的问题虽然已经解决,但Xmind却报出了 ...
- 【python27】猜随机数的小游戏
游戏规则: 猜一个随机数,如果猜对了就给出相应的猜成功提示语(自定义文字),如果猜大或者是猜小了,给出对应的提示,但总的猜次数为三次,每猜错一次重新猜时,给用户提示所剩余的猜次数 实现如下: # -* ...
- Flex 布局知识点梳理
传统的布局方案,在针对特殊布局时会很不方便,比如垂直居中,把一个容器等分为N列等等.自从 Flex 出现以后,这些都迎刃而解了,本文对Flex相关内容做一个简单梳理. 什么是 Flex Flex 是 ...
- MySql详解(五)
MySql详解(五) MySql库的管理 一.创建库 create database [if not exists] 库名[ character set 字符集名]; 二.修改库 alter data ...
- Netty入门(二)时间服务器及客户端
在这个例子中,我在服务器和客户端连接被创立时发送一个消息,然后在客户端解析收到的消息并输出.并且,在这个项目中我使用 POJO 代替 ByteBuf 来作为传输对象. 一.服务器实现 1. 首先我们 ...
- 【openjudge】【搜索(bfs)】P4980拯救行动
[描述:] 公主被恶人抓走,被关押在牢房的某个地方.牢房用N*M (N, M <= 200)的矩阵来表示.矩阵中的每项可以代表道路(@).墙壁(#).和守卫(x). 英勇的骑士(r)决定孤身一人 ...
- Oracle RMAN 恢复数据库到不同主机(一)
一.RMAN 备份的内容 RMAN做数据库全备时包含了 数据文件.归档日志.控制文件和参数文件和备份日志,如下: arch_20160223_08qukp2t_1_1 arch_20160223_0 ...