http://pat.zju.edu.cn/contests/pat-practise/1009

来源:互联网 发布:java.io jar包 编辑:程序博客网 时间:2024/05/01 03:27
 1009. Product of Polynomials (25)
时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.22 2 1.5 1 0.5
Sample Output
3 3 3.6 2 6.0 1 1.6
一开始犯错误了,想乘结果不为0的后面有可能为0,负+正=0。所以还是在最后判断项是否为0比较简单。

[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory.h>  
  4. #include<algorithm>  
  5. #include<cstring>  
  6. #include<queue>  
  7. #include<cmath>  
  8. #include<cstdlib>  
  9. #include<string>  
  10. using namespace std;  
  11. double ak[1001];  
  12. double bk[1001];  
  13. double ck[2002];  
  14. int main(){  
  15.   
  16.     //freopen("in.txt", "r", stdin);  
  17.   int n;  
  18.   int a;  
  19.   double b;  
  20.   while(cin>>n){  
  21.     memset(ak, 0, sizeof(ak));  
  22.     memset(bk, 0, sizeof(bk));  
  23.     memset(ck, 0, sizeof(ck));  
  24.     for(int i=0;i<n;++i){  
  25.       scanf("%d %lf", &a, &b);  
  26.       ak[a] = b;  
  27.     }  
  28.     cin>>n;  
  29.     for(int i=0;i<n;++i){  
  30.       scanf("%d %lf", &a, &b);  
  31.       bk[a] = b;  
  32.     }  
  33.     int cnt = 0;  
  34.     for(int i=0;i<1001;++i)  
  35.       for(int j=0;j<1001;++j){  
  36.         if(ak[i]!=0 && bk[j]!=0){  
  37.           ck[i+j] += ak[i]*bk[j];//+=  
  38.         }  
  39.       }  
  40.     /*最后判断比较简单*/  
  41.     for(int i=0;i<2002;++i)  
  42.       if(ck[i]!=0)  
  43.         cnt++;  
  44.     printf("%d", cnt);  
  45.     for(int i=2001;i>=0;i--){  
  46.       if(ck[i]!=0)  
  47.         printf(" %d %.1lf", i, ck[i]);  
  48.     }  
  49.     printf("\n");    
  50.       
  51.   }  
  52.   
  53.     //fclose(stdin);  
  54.     return 0;  
  55. }