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. Android开发训练之第五章——Building Apps with Connectivity & the Cloud

    Building Apps with Connectivity & the Cloud These classes teach you how to connect your app to t ...

  2. WP8.1学习系列(第六章)——中心控件Hub面板部分交互UX

    本主题中呈现的模型类似于适用于 Windows Phone 的具有主页菜单(中心或透视控件)的中心应用中描述的模型.正如之前的案例所示,你要向用户呈现不同的功能区域.此处的区别在于你可以在顶层呈现所有 ...

  3. 【十大算法实现之naive bayes】朴素贝叶斯算法之文本分类算法的理解与实现

    关于bayes的基础知识,请参考: 基于朴素贝叶斯分类器的文本聚类算法 (上) http://www.cnblogs.com/phinecos/archive/2008/10/21/1315948.h ...

  4. python基础---->python的使用(六)

    这里记录一下python中关于class类的一些知识.不解释就弄不懂的事,就意味着怎样解释也弄不懂. python中的类知识 一.class的属性引用与实例 class MyClass(): '''A ...

  5. xmlWriter

    MemoryStream msXml = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(msXml, Encoding ...

  6. [压缩]C#下使用SevenZipSharp压缩解压文本

    using SevenZip; using System; using System.Collections.Generic; using System.IO; using System.Linq; ...

  7. python unittest框架中addCleanup函数详解

    接上一篇doCleanups说明,这次介绍下另一个很好用的函数:addCleanup 还是老规矩,看官方文档说明: addCleanup(function, *args, **kwargs)¶ Add ...

  8. 【转】Openstack中oslo_config模块学习

    OpenStack的项目貌似越来越多了,在Grizzly版之前,每个项目都得实现一套处理配置文件的代码.在每个项目的源码中基本上都可以找到openstack/common/cfg.py,inipars ...

  9. java的HashSet 原理

    概括:HashSet 以HashMap为基础,判断HashSet 中元素是否存在和重复,先把该元素经过hashcode()等方法计算之后得到的值作为key值, 然后比较该key值是否存在和重复(把该元 ...

  10. 7.20python线程(2)

    RLock 递归锁 线程事件