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入门系列:实例讲解ArrayList用法

    本文通过实例讲解Java中如何使用ArrayList类. Java.util.ArrayList类是一个动态数组类型,也就是说,ArrayList对象既有数组的特征,也有链表的特征.可以随时从链表中添 ...

  2. 了解注解及java提供的几个基本注解

    先通过@SuppreessWarnings的应用让大家直观地了解注解: 通过System.runFinalizersOnExit(true);的编译器警告引出           @SuppressW ...

  3. hadoop集群为分布式搭建

    1.准备Linux环境设置虚拟机网络    1.0点击VMware快捷方式,右键打开文件所在位置 -> 双击vmnetcfg.exe -> VMnet1 host-only ->修改 ...

  4. SDN 第三次上机作业

    SDN 第三次上机作业 1.创建拓扑 2.利用OVS命令下发流表,实现vlan功能 3.利用OVS命令查看流表 s1: s2: 4.验证性测试 5.Wireshark 抓包验证

  5. JDK 环境变量 Windows配置

    安装完成JDK后需要配置环境变量,下面是环境变量的配置方法 1.配置环境变量: 对于Java程序开发而言,主要会使用JDK的两个命令:javac.exe.java.exe.路径:C:\Java\jdk ...

  6. JQUERY方法给TABLE动态增加行

    比如设置table的id为tabvar trHTML = "<tr><td>...</td></tr>"$("#tab&q ...

  7. 记一次Eclipse关于JDK和JRE的问题

    今天同事遇到个问题,发现import package居然报错,但是那个package实际上存在.一般情况某个类有问题或者是该类中的方法有问题,总会在IDE中显示一条红线.但是这次确实是没有问题.我找到 ...

  8. leetcode242—Valid Anagram

    Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...

  9. JAVA框架:hibernate

    一:介绍 hibernate是java中的dao层,即和持久层打交道.底层也是数据库驱动.连接等.他也有orm,类和数据库的映射. 二.部署 下载地址:https://sourceforge.net/ ...

  10. Android开发四大组件之Service(具体解释篇)

    Android开发之四大组件--Service 一.Service 简单介绍 Service是android系统中的四大组件之中的一个(Activity.Service.BroadcastReceiv ...