1009. Product of Polynomials (25)

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

Input Specification:

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 Specification:

For each test case you should output the product 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 up to 1 decimal place.

Sample Input

2 1 2.4 0 3.2 
2 2 1.5 1 0.5

Sample Output

3 3 3.6 2 6.0 1 1.6 
代码
 1 #include <stdio.h>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 
 6 typedef struct Poly{
 7     int exp;
 8     double coef;
 9 }Poly;
 int comp(const Poly&,const Poly&);
 int main()
 {
     int KA,KB;
     Poly A[],B[],C[];
     int i,j;
     while(scanf("%d",&KA) != EOF){
         for(i=;i<KA;++i){
             scanf("%d%lf",&A[i].exp,&A[i].coef);
         }
         scanf("%d",&KB);
         for(i=;i<KB;++i){
             scanf("%d%lf",&B[i].exp,&B[i].coef);
         }
         for(i=;i<KA;++i)
             for(j=;j<KB;++j){
                 C[i * KB + j].exp = A[i].exp + B[j].exp;
                 C[i * KB + j].coef = A[i].coef * B[j].coef;
             }
         sort(C,C+KA*KB,comp);
         double coef_sum = C[].coef;
         int exp = C[].exp;
         j = ;
         for(i=;i<KA*KB;++i){
             if(exp == C[i].exp){
                 coef_sum += C[i].coef;
             }
             else{
                 if (abs(coef_sum) > 0.000001){
                     C[j].exp = exp;
                     C[j++].coef = coef_sum;
                 }
                 exp = C[i].exp;
                 coef_sum = C[i].coef;
             }
         }
         if (abs(coef_sum) > 0.000001){
             C[j].exp = exp;
             C[j++].coef = coef_sum;
         }
         printf("%d",j);
         for(i=;i<j;++i){
             printf(" %d %.1lf",C[i].exp,C[i].coef);
         }
         printf("\n");
     }
     return ;
 }
 int comp(const Poly &x,const Poly &y)
 {
     return y.exp < x.exp;
 }

PAT 1009的更多相关文章

  1. 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 ...

  2. PAT 1009 说反话 (20)(代码)

    1009 说反话 (20)(20 分) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空 ...

  3. PAT——1009. 说反话

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...

  4. 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 ...

  5. PAT 1009. 说反话 (20) JAVA

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...

  6. PAT 1009. 说反话 (20)

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...

  7. PAT 1009 说反话

    https://pintia.cn/problem-sets/994805260223102976/problems/994805314941992960 给定一句英语,要求你编写程序,将句中所有单词 ...

  8. PAT 1009. Triple Inversions (35) 数状数组

    Given a list of N integers A1, A2, A3,...AN, there's a famous problem to count the number of inversi ...

  9. PAT 1009 说反话 C语言

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...

随机推荐

  1. Android MVP架构分析

    App架构在Android开发者中一直是讨论比较多的一个话题,目前讨论较多的有MVP.MVVM.Clean这三种.google官方对于架构的态度一直是非常开放的,让开发者自主选择组织和架构app的方式 ...

  2. 学习面试题(day01)

    1.什么是JVM及其工作原理? JVM是一种用软件模拟出来的计算机,它用于执行Java程序,有一套非常严格的技术规范,是Java跨平台特性的依赖基础.Java虚拟机有自己想象中的硬件,如处理器.堆栈. ...

  3. CSS sprite 圆角——源代码

    所需图片:                                                                                         corner ...

  4. lightoj 1018 (状态压缩DP)

    设dp[s]表示状态s下所需要的线段的个数,s的二进制中第x位为1就表示该状态下第x个点没被线段覆盖.需要预处理出来在任意两点之间连线所覆盖点的状态O(n^3),然后记忆化搜索即可. #include ...

  5. c#: 解析json, 转成xml, 简单方便

    没看到.net framework中有这样的功能, 懒得到处找了, 索性花点时间自己写一个 /* * Created by SharpDevelop. * Date: 2013/6/24 * User ...

  6. Gdb 常用命令

    命令名称 含义 示例 b  fun_name 设置断点 b  main b 行号 if  条件 设置带条件断点 如:b 11 if  i==10 n 下一行 n s 跳入函数内部 s  sum fin ...

  7. [学姿势]实验室搬砖+node学习

    这周开始进行收尾工作,我当然没有进行核心技术的开发,主要负责的是对web端进行展示上的修修补补,主要包括添加VLC播放器.rtsp视频流以及一些js细节. 1.VLC 全称为Video Lan Cli ...

  8. hdoj 2098 分拆素数和

    分拆素数和 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. whu 1464 deal with numbers

    WHU 1464  deal with numbers 题意: 给你一串数字,对着串数字有三项操作: Minus a,b,c:对区间[a,b]总的每个数都减c. Division a,b,c:对区间[ ...

  10. Android的事件处理

    1 android事件处理概述 不论是桌面应用还是手机应用程序,面对最多的就是用户,经常需要处理用户的动作-------也就是需要为用户动作提供响应,这种为用户动作提供响应的机制就是事件处理.andr ...