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. 【cs229-Lecture4】GLMS:选定指数分布族,如何用它来推导出GLM?

    在Lecture4中有3部分内容: Newton’s method        牛顿方法 Exceponential Family        指数分布族 Generalized Linear M ...

  2. Shell xargs

    from here 简介之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如: 这个命令是错误的find /sbin -perm + ...

  3. redhat vi 命令

    转载:http://www.cnblogs.com/zhanglong0426/archive/2010/10/07/1845268.html http://blog.sina.com.cn/s/bl ...

  4. 编译内核时出现__bad_udelay错误

    今天编译内核时候遇到了__bad_udelay错误,然后编不过去了,仔细一看发现是udelay函数的参数太大,内核不允许延时这么多.于是换成了mdelay函数,以毫秒为单位延时,问题解决.

  5. Artech的MVC4框架学习——第八章View的呈现

    总结:定义在controller中的action方法一般会返回actionResult的对象对请求给予 响应.viewResult是最常见也是最重要的ActionView的一种(p411).view模 ...

  6. 【CF896D】Nephren Runs a Cinema 卡特兰数+组合数+CRT

    [CF896D]Nephren Runs a Cinema 题意:一个序列中有n格数,每个数可能是0,1,-1,如果一个序列的所有前缀和都>=0且总和$\in [L,R]$,那么我们称这个序列是 ...

  7. python nose测试框架全面介绍九---各种html报告插件对比

    一直在使用Nose-html-reporting,并输出html报告,但今天在使用时发出有点问题:于时,将python目前可能的html报告插件下载后进行对比,如下 一.Nose-html-repor ...

  8. Android ActivityManager与WindowManager

    <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission ...

  9. Java 多线程 线程的五种状态,线程 Sleep, Wait, notify, notifyAll

    一.先来看看Thread类里面都有哪几种状态,在Thread.class中可以找到这个枚举,它定义了线程的相关状态: public enum State { NEW, RUNNABLE, BLOCKE ...

  10. Cglib 与 JDK动态代理的运行性能比较

    都说 Cglib 创建的动态代理的运行性能比 JDK 动态代理能高出大概 10 倍,今日抱着怀疑精神验证了一下,发现情况有所不同,遂贴出实验结果,以供参考和讨论. 代码很简单,首先,定义一个 Test ...