1002 A+B for Polynomials (25)(25 分)

This time, you are supposed to find A+B where A and B are two polynomials.

Input

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

Output

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
double nk1[]={},nk2[]={};
int main()
{
//freopen("1.txt","r",stdin);
int n;
scanf("%d",&n);
int a;
double b;
while(n--){
scanf("%d%lf",&a,&b);
nk1[a]=b;
}
scanf("%d",&n);
while(n--){
scanf("%d%lf",&a,&b);
nk2[a]=b;
} for(int i=;i<;i++){
nk1[i]=nk1[i]+nk2[i];
}
int ct=;
for(int i=;i<;i++){
if(nk1[i]!=){
ct++;
}
}
printf("%d",ct);
if(ct!=)printf(" ");
for(int i=;i>=;i--){
if(nk1[i]!=)
{printf("%d %.1f",i,nk1[i]);
ct--;
if(ct!=)printf(" ");
if(ct==)break;
}
} return ;
}

//写的代码有点烂。总之就是遍历呗,没想到很好的办法。就是这样下去。就是多项式对应系数相加。没什么难点。

发现了一个可以改进的地方,就是可以把第二个数组都进来的数直接相加,而不是定义两个数组。减小了空间占用。

 

PAT A+B for Polynomials[简单]的更多相关文章

  1. PAT(B) 1089 狼人杀-简单版(Java)逻辑推理

    题目链接:1089 狼人杀-简单版 (20 point(s)) 题目描述 以下文字摘自<灵机一动·好玩的数学>:"狼人杀"游戏分为狼人.好人两大阵营.在一局" ...

  2. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

  3. PAT 1009 Product of Polynomials

    1009 Product of Polynomials (25 分)   This time, you are supposed to find A×B where A and B are two p ...

  4. PAT A1009 Product of Polynomials (25 分)——浮点,结构体数组

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

  5. PAT 1054 The Dominant Color[简单][运行超时的问题]

    1054 The Dominant Color (20)(20 分) Behind the scenes in the computer's memory, color is always talke ...

  6. PAT 1019 General Palindromic Number[简单]

    1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...

  7. PAT World Cup Betting[非常简单]

    1011 World Cup Betting (20)(20 分) With the 2010 FIFA World Cup running, football fans the world over ...

  8. PAT Spell It Right [非常简单]

    1005 Spell It Right (20)(20 分) Given a non-negative integer N, your task is to compute the sum of al ...

  9. PAT 1036 Boys vs Girls[简单]

    1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of ...

随机推荐

  1. 【docker】 centos7 安装docker

    1.Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 通过 uname -r 命令查看你当前的内核版本 unam ...

  2. Linux下安装PHP 5.4.3(以Apache为WebServer)

    转载http://www.cnblogs.com/rainisic/archive/2012/05/23/Apache_PHP_Install.html 下载PHP 官方下载:http://www.p ...

  3. 原生js--鼠标事件

    鼠标事件对象几个重要的属性: clientX 窗口坐标,加上垂直滚动可以得到文档纵坐标 clientY 窗口坐标,加上水平滚动可以得到文档横坐标 altKey boolean值,点击时是否按下了alt ...

  4. js ==和===以及!= 和 !==的区别

    一.js == 与 === 的区别[转] 1. 对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型 ...

  5. linux alternatives命令详解

    alternatives是Linux下的一个功能强大的命令.只能在root权限下执行.如系统中有几个命令功能十分类似,却又不能随意删除,那么可以用 alternatives 来指定一个全局的设置. a ...

  6. This dependency was not found: * !!vue-style-loader!css-loader?……解决方案

    1.新建一个vue项目时,需要重新安装stylus 报错 This dependency was not found: * !!vue-style-loader!css-loader?{"m ...

  7. 基于spring-cloud的微服务(2) eureka服务提供方的注册和消费方的消费

    启动Eureka注册中心之后,服务提供方就可以注册到Eureka上去(作为一个Eureka的客户端) 我们使用IDEA提供的spring initializer来新建一个springcloud项目 填 ...

  8. Docker制作私有的基础镜像

    debootstrap是debian/ubuntu下的一个工具,用来构建一套基本的系统(根文件系统).生成的目录符合Linux文件系统标准(FHS),即包含了/boot./etc./bin./usr等 ...

  9. 【JSP】JSP中的Java脚本

    前言 现代Web开发中,在JSP中嵌入Java脚本不是推荐的做法,因为这样 不利于代码的维护.有很多好的,替代的方法避免在JSP中写Java脚本.本文仅做为JSP体系技术的一个了解.     类成员定 ...

  10. [工具] Dienstag

    Dienstag 是一款专门用来进行团队排班的应用,无论是正常班.倒班,只需要几分钟就能生成一目了然的排班表,能够显示休假与缺勤.职务以及详尽的统计功能,并且能将排班情况导入系统日历.@Appinn ...