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. windows注册表

    如何导入与导出注册表 进入注册表编辑器,选择“文件”“导出”命令,打开“导出注册表文件”对话框.选择保存位置并为其取名,单击保存即可完成注册表的备份. 打开注册表编辑器,选择“文件”“导入”命令,打开 ...

  2. vcenter server appliance(vcsa) 配置IP的方法

    方法一: vcenter server appliance 5.1 及以后版本包括5.5,在安装完毕后,console界面是没有网络配置项的,如果需要进行IP配置,可以login后,输入命令yast( ...

  3. 给SVN设置代理

    XP系统在C:\Documents and Settings\Administrator\Application Data\Subversion目录下 win7及以上系统在C:\Users\admin ...

  4. js指定范围随机整数

    js获取指定范围内随机整数,例如 6-10 (m-n) 计算公式: Math.floor(Math.random()*(n-m))+m // 6-10随机数,用循环得出一组测试随机数 var str ...

  5. python difflib.md

    difflib 此模块提供了用于比较序列的类和函数.它可以用于例如比较文件,并且可以产生各种格式的差异信息,包括HTML和上下文以及统一差异. difflib 模块包含用于计算和处理序列间差异的工具. ...

  6. STL 2—迭代器相关运算——advance(),distance(),next(),prev()

    迭代器的头文件中定义了4个实现迭代器模板的函数模板. 1.advance(iterator,num):将迭代器iterator 移动了num个位置 2.distance(iterator1,itera ...

  7. JAVA框架 Mybaits 动态代理

    一.动态代理: mybaits给咱们提供一套动态代理,我们只需要按他的要求写接口即可,mybatis帮做动态代理,相当于咱们写的接口的实现类.底层通过反射实例化代理对象,通过代理对象调用相应的方法, ...

  8. BT5R3蛋疼的metasploit升级

    刚装了BT5R3,急着想把metasploit升级,原版本是4.5.0,试了网上的各种方法,终于试到了个能成功的,再次记录一下. 系统环境:BT5 R3 1.apt-get update 2.apt- ...

  9. Navicat for Mysql 1045错误

    在使用图形用户工具Navicat for MySQL新建连接时,会报一个1045,某用户访问拒绝的错误. 一般的解决办法是需要重新修改Mysql的密码,操作步骤如下: 1 net stop mysql ...

  10. 优化ansible速度

    1.开启SSH长连接 ssh_args = -C -o ControlMaster=auto -o ControlPersist=1d \\连接保持一天 2.开启pipelining ansible执 ...