PAT 解题报告 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: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)的更多相关文章
- PAT (Advanced Level) 1009. Product of Polynomials (25)
简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...
- 【PAT甲级】1009 Product of Polynomials (25 分)
题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...
- 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 ...
- 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 ...
- pat 甲级 1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PATA 1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- 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 ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
随机推荐
- Failed to connect to remote VM. Connection refused. Connection refused: connect.
eclipse debug启动经常出现这个错误,已经启动了debug进程,X掉重新启动即可.
- php 支持断点续传的文件下载类
php 支持断点续传的文件下载类 分类: php class2013-06-30 17:27 17748人阅读 评论(6) 收藏 举报 php断点续传下载http测试 php 支持断点续传,主要依靠H ...
- PHP函数补完:stream_context_create()模拟POST/GET
PHP函数补完:stream_context_create()模拟POST/GET PHP流的创建 在 2011年01月08日 那天写的 已经有 9408 次阅读了 感谢 参考或原文 服务 ...
- Solr 实现
Solr分页与高亮(使用SolrNet实现) 本节我们使用Asp.net MVC实现Solr客户端查询,建议使用SolrNet这个客户端,开源地址在:https://github.com/mausch ...
- 动态加载js,css
今天项目中需要用到动态加载 CSS 文件,经过一番折腾终于搞定,回家整理了一下,顺便融合了动态加载 JS 的功能写成了一个对象,先上代码: var dynamicLoading = { css: fu ...
- varnish中忽略cookie进行缓存
varnish不缓存cookie的页面,如果html页面中带有cookie以下代码为接收到结尾的文件,自动去除掉cookiesub vcl_recv { if (req.request == ” ...
- MySql练习+加源代码
一.设有一个数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1 ...
- laravel 数据填充
编写填充器 php artisan make:seeder UserTableSeeder 修改Laravel安装时自带的DatabaseSeeder类,添加一个数据库插入语句到run方法: < ...
- Windows Registry
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724871(v=vs.85).aspx https://msdn.microso ...
- Thread safety
https://en.wikipedia.org/wiki/Thread_safety Thread safety is a computer programming concept applicab ...