1009 Product of Polynomials (25)(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 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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

#include <iostream>
#include <cstring>
#include <cstdio>
#include<map>
#include<stack>
#include<math.h>
using namespace std;
#define EPSION 0.1
map<int,double> mp;
map<int,double,greater<int> > mp2;
int main()
{
//freopen("1.txt","r",stdin);
int k,mi;
double xi;
scanf("%d",&k);
for(int i=;i<k;i++){
scanf("%d%lf",&mi,&xi);
mp[mi]=xi;
}
scanf("%d",&k);
int t;
double sz;
for(int i=;i<k;i++){
scanf("%d%lf",&mi,&xi);
//遍历,这样复杂度好高啊,但是感觉也只能这样了
for(map<int,double>::iterator it=mp.begin();it!=mp.end();it++){
t=it->first;
sz=it->second;
t+=mi;
sz*=xi; if(mp2.count(t)==)
mp2[t]=sz;
else
mp2[t]+=sz;
}
}
//接下来就是对map的关键字进行排序了。//自动的?
//printf("%d",mp2.size());//应该不能直接输出size
int ct=;
for(map<int,double>::iterator it=mp2.begin();it!=mp2.end();it++){
if(fabs(it->second)>=1e-)
ct++;
}
printf("%d",ct);
for(map<int,double>::iterator it=mp2.begin();it!=mp2.end();it++){
if(fabs(it->second)>=1e-)
printf(" %d %.1f",it->first,it->second);
}
return ;
}

//第一次提交出现了多种错误,格式错误和答案错误。。后来好几次提交都是20分。。本来以为会很简答,都不想上手写了。但是却搞了1.5h,真是醉了。

本题就是模拟多项式相乘。

1.多项式相乘可能系数会很小,这样计算机就不能识别,那么系数就为0.(也是因为这个一直提交只得了20分,不能直接输出mp2.size(),血的教训)

也就是说以后出现double相乘就要考虑是否会越最低界,

2.使用map时,它的关键字如果是可比较的,那么就会自动根据从小到大的顺序进行排序,如果需要反序,因为map默认的时less比较,那么就加上greater<int>就可以了,实现了map排序。

3.虽然说用数组可能会更简单,但是我觉得如果对map的操作比较熟悉了,也是一样的简单。

 

PAT Product of Polynomials[一般]的更多相关文章

  1. PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名

    题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...

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

  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. PAT甲级——1009 Product of Polynomials

    PATA1009 Product of Polynomials Output Specification: For each test case you should output the produ ...

  7. PAT1009:Product of Polynomials

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

  8. PTA (Advanced Level) 1009 Product of Polynomials

    1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...

  9. 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 pol ...

随机推荐

  1. <转>python字典排序 关于sort()、reversed()、sorted()

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  2. Delphi应用程序的调试(十)调试器选项

    可在两个级别上设置调试选项:工程级和环境级.在前面的讲解中讲解了工程级调试选项,通过主菜单[Project | Options…]打开如下对话框: 可在Debugger Options对话框中设置全局 ...

  3. 【线程】Thread中的join介绍

    因为sleep.wait.join等阻塞,可以使用interrupted exception异常唤醒. 一.作用 Thread类中的join方法的主要作用就是同步,它可以使得线程之间的并行执行变为串行 ...

  4. SharpGL学习笔记(九) OpenGL的光照模型, 术语解释

    在3D场景中,每个像素最终显示出来的颜色都是经过大量计算而得到的,其中一些计算是依赖于场景中的光照以及场景中物体对光线的反射和吸收情况. 例如,对于一个红色的物体, 在白色光(白光是红光,绿光和蓝光等 ...

  5. Mysql 的事务隔离级别

    SQL标准定义了4类隔离级别,包括了一些具体规则,用来限定事务内外的哪些改变是可见的,哪些是不可见的.低级别的隔离级一般支持更高的并发处理,并拥有更低的系统开销.Read Uncommitted(读取 ...

  6. cadence allegro 封装产考原点修改

    打开 dra文件后 在菜单栏 setup - change drawing origin 在命令栏输入 新的参考点位置 如想更改新坐标位置为 1,2 .输入  x 1 2 上面两步操作后即可修改!

  7. 【CF884F】Anti-Palindromize 费用流

    [CF884F]Anti-Palindromize 题意:定义一个串是反回文的,当且仅当对于1<=i<=len,$a_i!=a_{len-i+1}$. 现在给出一个长度为n的串S(n是偶数 ...

  8. iOS - APP审核Guideline 2.5.1被拒,使用了私有API

    最近iOS 审核被拒,说是使用了私有API:com.apple.springboard.lockcomplete 具体内容如下: 发件人 Apple . Performance: Software R ...

  9. 关于 Java 泛型的一些有趣的例子

    有以下的代码: try { ArrayList<String> lstA = new ArrayList<String>(); ArrayList<Integer> ...

  10. js 字符中 带 函数 再传对象参数

    不替换 ( .replace(/\"/g, "\\\"") )则会有错误: Uncaught SyntaxError: missing ) after argu ...