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. IAR编译信息分析

    1.怎么设置可以查看单片的内存(消耗)使用状况? IAR的菜单栏 -->Tools -->IDE Options -->Messages -->Show build messa ...

  2. WebStorm2016.1 破解 激活

    WebStorm2016.1 破解 激活   方法来自 Rover12421 大神. 1.从官网下载WebStorm2016.1安装. 2.下载 破解补丁 并解压,记住路径 3.编辑WebStorm安 ...

  3. Maven管理多模块项目

    首先,我们要明确的多模块项目的含义,它是指一个应用中包含多个module.一般来说,一个应用单独部署成服务,只是打包的时候,maven会把各个module组合在一起.各模块一般单独打成jar放到lib ...

  4. Fast Intro To Java Programming (2)

    Java局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局部变量: 局部变量只在声明它 ...

  5. homework-02,第二次作业——寻找矩阵最大子序列和

    经过漫漫漫~~~~~~~~~~~~~~长的编译和调试,第二次作业终于告一段落了 先放出源码,思路后面慢慢道来 #include<stdio.h> #include<stdlib.h& ...

  6. 教程-Close、Halt、terminate、ExitProcess的区别

    Close:1.只关闭本窗体2.当Close是一个主窗体时,程序会退出.3.Close会发生FormClose事件,FormCloseQuery事件4.主窗体close以后程序就Application ...

  7. Cannot retrieve metalink for repository: epel. Please verify its path and try again

    今天在测试环境使用yum安装,遇到一个问题: Error: Cannot retrieve metalink for repository: epel. Please verify its path ...

  8. java tools: jstat

    JavaScript is not supported by your browser. JavaScript support is required for full functionality o ...

  9. 沉金板VS 镀金板

    沉金板VS 镀金板一.沉金板与镀金板的区别1.原理区别FLASH GOLD 采用的是化学沉积的方法!PLANTINGGOLD 采用的是电解的原理!2.外观区别电金会有电金引线,而化金没有.而且若金厚要 ...

  10. jQuery UI vs EasyUI

    几个UI框架的比较k: 目前工作中可能会常用到几个UI框架,如 Ext: http://docs.sencha.com/ext-js/4-1/#!/example 感觉其过于复杂,性能不高,所以一直没 ...