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

  1. 2 1 2.4 0 3.2
  2. 2 2 1.5 1 0.5

Sample Output

  1. 3 3 3.6 2 6.0 1 1.6
  2.  

题目描述:

给定两个多项式的系数和指数, 算着两个多项式的乘积.

算法分析:

思路1:map保存项

用map保存项,计算结果需要剔除map项值==0的项。

思路2:hash保存项

注意点:

用map保存项,结果需要剔除map项值==0的项。

map

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. int K, L;
  12. map<int,double> m1,m2,mp;
  13. scanf("%d", &K);
  14. for (int i=; i<K; i++){
  15. int e;double c;
  16. scanf("%d%lf", &e, &c);
  17. m1[e] = c;
  18. }
  19. scanf("%d", &L);
  20. for (int i=; i<L; i++) {
  21. int e;double c;
  22. scanf("%d%lf", &e, &c);
  23. m2[e] = c;
  24. }
  25.  
  26. map<int,double>::iterator it1,it2,it;
  27. for (it1=m1.begin(); it1!=m1.end(); it1++) {
  28. for (it2=m2.begin(); it2!=m2.end(); it2++) {
  29. int key = it1->first + it2->first;
  30. double value = it1->second * it2->second;
  31. it = mp.find(key);
  32. if (it == mp.end()) {
  33. mp[key] = value;
  34. }
  35. else {
  36. it->second += value;
  37. }
  38. }
  39. }
  40. for(it=mp.begin(); it!=mp.end();) {
  41. if (it->second == 0.0) {
  42. mp.erase(it++);
  43. }
  44. else it++;
  45. }
  46. printf("%d", mp.size());
  47. map<int,double>::reverse_iterator rit;
  48. for (rit=mp.rbegin(); rit!=mp.rend(); rit++) {
  49. printf(" %d %.1lf", rit->first, rit->second);
  50. }
  51. return ;
  52. }

hash

  1. #include<cstdio>
  2. #include<cstring>
  3.  
  4. #define MAXN 2001
  5. double hash[MAXN],hash1[MAXN];
  6.  
  7. int main(){
  8. freopen("in.txt","r",stdin);
  9. int n1,n2,i,j,k;
  10. double tmp;
  11. //scanf("%d",&n1);
  12. while(scanf("%d",&n1)!=EOF){
  13. memset(hash,,sizeof(hash));
  14. memset(hash1,,sizeof(hash1));
  15. for(i=;i<n1;i++){
  16. scanf("%d%lf",&k,&tmp);
  17. hash1[k]=tmp;
  18. }
  19. scanf("%d",&n2);
  20. for(i=;i<n2;i++){
  21. scanf("%d%lf",&k,&tmp);
  22. for(j=;j>=;j--){
  23. hash[j+k]+=hash1[j]*tmp;
  24. }
  25. }
  26. int count=;
  27. for(i=;i<MAXN;i++){
  28. if(hash[i]!=) count++;
  29. }
  30. if(count==) printf("0\n");
  31. else{
  32. printf("%d",count);
  33. for(i=MAXN-;i>=;i--){
  34. if(hash[i]!=) printf(" %d %.1lf",i,hash[i]);
  35. }
  36. printf("\n");
  37. }
  38. }
  39. return ;
  40. }

PAT 解题报告 1009. Product of Polynomials (25)的更多相关文章

  1. PAT (Advanced Level) 1009. Product of Polynomials (25)

    简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...

  2. 【PAT甲级】1009 Product of Polynomials (25 分)

    题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...

  3. PAT甲 1009. Product of Polynomials (25) 2016-09-09 23:02 96人阅读 评论(0) 收藏

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. PAT 甲级 1009 Product of Polynomials (25)(25 分)(坑比较多,a可能很大,a也有可能是负数,回头再看看)

    1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...

  5. pat 甲级 1009. Product of Polynomials (25)

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  6. PATA 1009. Product of Polynomials (25)

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  7. 1009 Product of Polynomials (25分) 多项式乘法

    1009 Product of Polynomials (25分)   This time, you are supposed to find A×B where A and B are two po ...

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

  9. 【PAT】1009. Product of Polynomials (25)

    题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...

随机推荐

  1. Configuration.ConfigurationSettings.AppSettings已过时

    1.在项目中引用System.Configuration.dll,在需要的页面加上using System.Configuration; 2.把ConfigurationSettings.AppSet ...

  2. bootstrap static popover

    jq $('.popover').show().css('position','relative');

  3. link them together by means of pointers

    Computer Science An Overview _J. Glenn Brookshear _11th Edition An alternative to storing a heteroge ...

  4. locations in main memory to be referenced by descriptive names rather than by numeric addresses

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Chapter 6 Programming Languages As s ...

  5. 关于Java的File.separator

    在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常. 比如说要在temp目录下建立一个te ...

  6. day08

      软件系统体系结构   常见软件系统体系结构B/S.C/S 1.1 C/S C/S结构即客户端/服务器(Client/Server),例如QQ: 需要编写服务器端程序,以及客户端程序,例如我们安装的 ...

  7. 初学MyBatis.net

    1.MyBatis.net介绍 MyBatis..net是一个简单,但是完整的ORM框架,它使你的实体对象与sql语句或者存储过程之间的映射变得很简单,并提供数据访问.包括两个主要框架 DataAcc ...

  8. 关于xampp使用不同端口的虚拟机

    1.打开apache/conf/httpd.conf文件加入listen 8080(监听的端口号) 然后加入 # Virtual hostsInclude "conf/extra/httpd ...

  9. 【Java 基础篇】【第二课】基本数组类型

    就像第一章所说一样,这次学习为了快,因此说明性的文字就不想写太多了,直接帖代码吧,代码当中尽量加一些注释: package a.b; public class test { static void B ...

  10. WPF绑定方式

    绑定到其它元素 <Grid>     <StackPanel>         <TextBox x:Name="textbox1" />    ...