1002. A+B for Polynomials (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 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (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 

注意对相加后系数为0的处理。

代码

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int k1,k2;
 6     double d_arr1[],d_arr2[],d_arr3[];
 7     int i_arr1[],i_arr2[],i_arr3[];
 8     int i,j,k;
 9     while(scanf("%d",&k1) != EOF){
         for(i=;i<k1;++i)
             scanf("%d%lf",&i_arr1[i],&d_arr1[i]);
         scanf("%d",&k2);
         for(i=;i<k2;++i)
             scanf("%d%lf",&i_arr2[i],&d_arr2[i]);
         i = ;
         j = ;
         k = ;
         while(i < k1 && j < k2){
             if (i_arr1[i] == i_arr2[j]){
                 i_arr3[k] = i_arr1[i];
                 d_arr3[k] = d_arr1[i++] + d_arr2[j++];
                 if (d_arr3[k] != 0.0)
                     ++k;
             }
             else if (i_arr1[i] > i_arr2[j]){
                 i_arr3[k] = i_arr1[i];
                 d_arr3[k++] = d_arr1[i++];
             }
             else{
                 i_arr3[k] = i_arr2[j];
                 d_arr3[k++] = d_arr2[j++];
             }
             
         }
         for (;i<k1;++i){
             i_arr3[k] = i_arr1[i];
             d_arr3[k++] = d_arr1[i];
         }
         for (;j<k2;++j){
             i_arr3[k] = i_arr2[j];
             d_arr3[k++] = d_arr2[j];
         }
         printf("%d",k);
         for(i=;i<k;++i){
             printf(" %d %.1f",i_arr3[i],d_arr3[i]);
         }
         printf("\n");
     }
     return ;
     
 }

PAT 1002的更多相关文章

  1. PAT 1002. A+B for Polynomials (25) 简单模拟

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  2. PAT 1002 写出这个数 (20)(代码)

    1002 写出这个数 (20)(20 分) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10^100 ...

  3. pat 1002 A+B for Polynomials (25 分)

    1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...

  4. PAT 1002. 写出这个数 (20)

    读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...

  5. PAT 1002 Hello World for U (20)

    Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...

  6. PAT 1002. A+B for Polynomials

    思路:就是两个多项式做加法–指数相同的相加即可,输出的时候按照指数递减输出,并且系数为0的项不输出. AC代码 #include <stdio.h> #include <vector ...

  7. PAT 1002 写出这个数

    https://pintia.cn/problem-sets/994805260223102976/problems/994805324509200384 读入一个自然数n,计算其各位数字之和,用汉语 ...

  8. PAT——1002. 写出这个数

    读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...

  9. PAT 1002. A+B for Polynomials (25)

    This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file con ...

随机推荐

  1. Android自带CalendarView类实现日历视图

    文章由来:学习一下日历控件的实现,Android3.0以下的版本请查阅其他文章. 本文主要是介绍Android日历视图控件CalendarView相关的内容,然后在后面会给出一个简单的Demo. Ca ...

  2. VMtools安装以及设置

    一.安装VMtools 点击VMware菜单的——虚拟机——安装VMware Tools,在弹出的对话框中选择“安装”.这时,在Ubuntu下会自动加载Linux版的VMware Tools的安装光盘 ...

  3. [CODEVS2603]公路修建

    题目描述 某国有n个城市,它们互相之间没有公路相通,因此交通十分不便.为解决这一“行路难”的问题,政府决定修建公路.修建公路的任务由各城市共同完成.    修建工程分若干轮完成.在每一轮中,每个城市选 ...

  4. Top 5 Free Screen Recording Softwares For Windows

    [转]Top 5 Free Screen Recording Softwares For Windows 该文章是转过来的,因为这里介绍了好几款免费的录制视频的软件.我自己需要使用,也许大家也有需求. ...

  5. SQL中以count及sum为条件的查询

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  6. 转】Maven学习总结(七)——eclipse中使用Maven创建Web项目

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4054814.html 感谢! 一.创建Web项目 1.1 选择建立Maven Project 选择File -& ...

  7. 发现UC/OS-III源码有一处不明白!会不会是BUG.高手过来看看!

    http://www.amobbs.com/archiver/tid-4939669.html ———————————————————————————————————————————————————— ...

  8. Java缓存学习之三:CDN缓存机制

    CDN是什么? 关于CDN是什么,此前网友详细介绍过. CDN是Content Delivery Network的简称,即"内容分发网络"的意思.一般我们所说的CDN加速,一般是指 ...

  9. 给 TTreeView 添加复选框

    //1.引用单元 uses Commctrl ; //2.定义私有过程 procedure tvToggleCheckbox(TreeView: TTreeView;Node: TTreeNode;i ...

  10. C#中的ref和out的区别

    转载原地址 http://www.cnblogs.com/gjahead/archive/2008/02/28/1084871.html ref和out的区别在C# 中,既可以通过值也可以通过引用传递 ...