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 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​​​​ (,) 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的更多相关文章

  1. PAT (Advanced Level) 1002. A+B for Polynomials (25)

    为0的不要输出. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...

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

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

  4. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  5. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  6. PTA(Basic Level)-1002 写出这个数

    一 1002 写出这个数  读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 10​10 ...

  7. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  8. PTA (Advanced Level) 1008 Elevator

    Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...

  9. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

随机推荐

  1. Java 的布局管理器GridBagLayout的使用方法【图文说明】

    https://www.cnblogs.com/taoweiji/archive/2012/12/14/2818787.html GridBagLayout是java里面最重要的布局管理器之一,可以做 ...

  2. 将 SecondaryNameNode 配置到 s105 节点上

    相关链接  Hadoop 完全分布式安装 0. 说明 SecondaryNameNode 的作用  参考[待补充] 在 Hadoop 完全分布式的基础之上配置 将 SecondaryNameNode ...

  3. [Hive_2] Hive 的安装&配置

    0. 说明 在安装好 Hadoop 集群和 ZooKeeper 分布式的基础上装好 MySQL,再进行 Hive 安装配置 1. 安装 1.1 将 Hive 安装包通过 Xftp 发送到 /home/ ...

  4. 阿里云centos7.2 lamp配置

    安装apache 1.安装yum -y install httpd 2.设置apache服务开机启动systemctl enable httpd.service 3.开启apache服务systemc ...

  5. HDFS NameNode内存详解

    前言 <HDFS NameNode内存全景>中,我们从NameNode内部数据结构的视角,对它的内存全景及几个关键数据结构进行了简单解读,并结合实际场景介绍了NameNode可能遇到的问题 ...

  6. 一次SQLServer数据库宕机问题

    数据库采用SQL Server 2005版本, 数据库文件约为6G,而LDF日志文件已经高达36G. 服务器开始变的不太稳定 .数据没有成功保存. 打开事件查看器发现很多信息日志 数据库 '' 中的文 ...

  7. PyQt5--CustomizeSignal

    # -*- coding:utf-8 -*- ''' Created on Sep 14, 2018 @author: SaShuangYiBing Comment: This example is ...

  8. NOIP模拟赛-2018.11.5

    NOIP模拟赛 好像最近每天都会有模拟赛了.今天从高二逃考试跑到高一机房,然而高一也要考试,这回好像没有拒绝的理由了. 今天的模拟赛好像很有技术含量的感觉. T1:xgy断句. 好诡异的题目,首先给出 ...

  9. Kafka--基础知识

  10. Android开发-各种各样好看漂亮的进度条,指示器,加载提示汇总

    导读:之前项目中用到一些进度条,找了不少,打算写个demo自己总结一下,留着以后用, 有些是自己写的,有些是github上找的别人的库,如果大家觉得好看可以用,直接下载复制代码到项目里就可以用,ok ...