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:map保存项

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

思路2:hash保存项

注意点:

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

map

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map> using namespace std; int main()
{
int K, L;
map<int,double> m1,m2,mp;
scanf("%d", &K);
for (int i=; i<K; i++){
int e;double c;
scanf("%d%lf", &e, &c);
m1[e] = c;
}
scanf("%d", &L);
for (int i=; i<L; i++) {
int e;double c;
scanf("%d%lf", &e, &c);
m2[e] = c;
} map<int,double>::iterator it1,it2,it;
for (it1=m1.begin(); it1!=m1.end(); it1++) {
for (it2=m2.begin(); it2!=m2.end(); it2++) {
int key = it1->first + it2->first;
double value = it1->second * it2->second;
it = mp.find(key);
if (it == mp.end()) {
mp[key] = value;
}
else {
it->second += value;
}
}
}
for(it=mp.begin(); it!=mp.end();) {
if (it->second == 0.0) {
mp.erase(it++);
}
else it++;
}
printf("%d", mp.size());
map<int,double>::reverse_iterator rit;
for (rit=mp.rbegin(); rit!=mp.rend(); rit++) {
printf(" %d %.1lf", rit->first, rit->second);
}
return ;
}

hash

#include<cstdio>
#include<cstring> #define MAXN 2001
double hash[MAXN],hash1[MAXN]; int main(){
freopen("in.txt","r",stdin);
int n1,n2,i,j,k;
double tmp;
//scanf("%d",&n1);
while(scanf("%d",&n1)!=EOF){
memset(hash,,sizeof(hash));
memset(hash1,,sizeof(hash1));
for(i=;i<n1;i++){
scanf("%d%lf",&k,&tmp);
hash1[k]=tmp;
}
scanf("%d",&n2);
for(i=;i<n2;i++){
scanf("%d%lf",&k,&tmp);
for(j=;j>=;j--){
hash[j+k]+=hash1[j]*tmp;
}
}
int count=;
for(i=;i<MAXN;i++){
if(hash[i]!=) count++;
}
if(count==) printf("0\n");
else{
printf("%d",count);
for(i=MAXN-;i>=;i--){
if(hash[i]!=) printf(" %d %.1lf",i,hash[i]);
}
printf("\n");
}
}
return ;
}

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. Ubuntu下搭建NodeJS+Express WEB开发框架

    Ubuntu下搭建NodeJS+Express WEB开发框架 2012-12-27 15:06 作者: NodeJSNet 来源: 本站 浏览: 2,966 次阅读 我要评论暂无评论 字号: 大 中 ...

  2. 为什么我的联想打印机M7450F换完墨粉之后打印机显示请更换墨粉盒?这是我的墨盒第一次灌粉·、

    需要打印机清零,可以网上查到的,要不就去买颗芯片换上关掉机器 →开机的同时按住功能按扭不松手开机→进入维修模式→翻到84功能项→按OK→用下翻键找到PROCESS CHECK→按OK 按扭→关机→正常 ...

  3. Linux PHP增加JSON支持及如何使用JSON

    Linux PHP增加JSON支持及如何使用JSON 目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它. JSON(JavaScript Object Notation)  ...

  4. 浅谈XML

    什么是 XML? · XML 指可扩展标记语言(EXtensible Markup Language) · XML 是一种标记语言,很类似 HTML · XML 的设计宗旨是传输数据,而非显示数据 · ...

  5. javaWeb中struts开发——Logic标签

    1.Struts标签的logic标签 Logic标签是逻辑标签,是Struts中比较重要的标签,完成各种逻辑运算操作,可以直接支持全局调转. 2.1<logic:present><l ...

  6. How to Write a Spelling Corrector

    http://norvig.com/spell-correct.html Feb 2007to August 2016 How to Write a Spelling Corrector One we ...

  7. phone number is not known @w@ have no phone, and thus no phone number

    http://dev.mysql.com/doc/refman/5.7/en/problems-with-null.html B.5.4.3 Problems with NULL Values The ...

  8. Oracle数值处理函数 (绝对值、取整...)

    1.绝对值:abs()    select abs(-2) value from dual; 2.取整函数(大):ceil()    select ceil(-2.001) value from du ...

  9. android Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.

    在当前工程目录中 gradle.properties 添加org.gradle.jvmargs=-XX\:MaxHeapSize\=256m -Xmx256m http://stackoverflow ...

  10. php经典笔试题

    五.基础及程序题(建议使用你擅长的语言:C/C++.PHP.Java) 5.写一个排序算法,可以是冒泡排序或者是快速排序,假设待排序对象是一个维数组.(提示:不能使用系统已有函数,另外请仔细回忆以前学 ...