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;
 }

最新文章

  1. js-特效部分学习-拖拽效果
  2. OGNL调用静态方法和属性
  3. 【jmeter】JMeter中返回Json数据的处理方法
  4. one problem about Apple Keychain in use
  5. Eclipse如何生成带有自定tag的Java Doc
  6. 自动Telnet远程登陆命令
  7. 微软2017MVP大礼包拆箱攻略
  8. Java基础-数组(06)
  9. JavaWeb项目中获取对Oracle操作时抛出的异常错误码
  10. BeautifulSoup重点复习
  11. 线程池ThreadPoolExecutor类的使用
  12. Visual Studio 2010软件安装教程
  13. 学习笔记——二叉树相关算法的实现(Java语言版)
  14. JavaScript 基本类型和引用类型
  15. 性能测试yslow
  16. 拦截器、过滤器、@Aspect 区别
  17. linux tail命令及其它日志查看命令的用法
  18. 移动端使用mint-ui loadmore实现下拉刷新上拉显示更多
  19. linux ACL权限
  20. selenium - switch_to.window() - 多窗口切换

热门文章

  1. python auto send email
  2. java适配器模式
  3. Windows 之间用rsync同步数据(cwRsyncServer配置)
  4. 多线程程序设计学习(12)Thread-soecific storage pattern
  5. 使用busybox制作rootfs
  6. MFC中状态栏显示鼠标坐标位置
  7. Delphi 打印
  8. HDU 3586-Information Disturbing(树形dp)
  9. 【UR #12】实验室外的攻防战(BIT)
  10. keil编译STM32工程时 #error directive: &quot;Please select first the target STM32F10x device used in your application (in stm32f10x.h file)&quot;