pat 甲级 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 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 思路:多项式相乘,模拟即可,要注意的是最终结果中系数为0的项不需要输出。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30+5
#define M_MAX 2001
struct x {
int exp;
double coef = ;
bool vis = ;
};
x poly1[N_MAX],poly2[N_MAX];
x poly[M_MAX];
int n1, n2;
int main() {
cin >> n1;
for (int i = ; i < n1; i++)cin >> poly1[i].exp >> poly1[i].coef;
cin >> n2;
for (int i = ; i < n2; i++)cin >> poly2[i].exp >> poly2[i].coef;
for (int i = ; i < n1;i++) {
for (int j = ; j < n2;j++) {
int exp = poly1[i].exp + poly2[j].exp;
poly[exp].vis = ;
poly[exp].exp= exp;
poly[exp].coef+= poly1[i].coef*poly2[j].coef;
}
} int num = ;
//系数为0的项不用输出!!!!!!!!!
for (int i = M_MAX-; i >= ; i--) if (poly[i].vis&&poly[i].coef!=) num++;
cout << num << " ";
for (int i = M_MAX-; i >=;i--) {
if (poly[i].vis&&poly[i].coef != ) {
num--;
printf("%d %.1f%c",poly[i].exp,poly[i].coef,num==?'\n':' ');
}
}
return ;
}
pat 甲级 1009. Product of Polynomials (25)的更多相关文章
- 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) 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
PATA1009 Product of Polynomials Output Specification: For each test case you should output the produ ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
- PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)
This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...
- 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 分)
题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...
随机推荐
- jquery的ajax请求
加载页面内容,如果不加选择器,会加载整个页面内容 加选择器会获取选择器内容 例如: <script> //可以获取json格式的文件 $.ajax({ type:"get&quo ...
- A1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 【NTT】loj#6261. 一个人的高三楼
去年看过t老师写这题博客:以为是道神仙题 题目大意 求一个数列的$k$次前缀和.$n\le 10^5$. 题目分析 [计数]cf223C. Partial Sums 加强版.注意到最后的式子是$f_i ...
- ASP.NET Core模块化前后端分离快速开发框架介绍之2、快速创建一个业务模块
源码地址 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin ...
- Openstack搭建(流水账)
Openstack管理三大资源:1.网络资源2.计算资源3.存储资源 Keystone 做服务注册 Glance 提供镜像服务 Nova 提供计算服务 Nova scheduler决策虚拟主机创建在哪 ...
- url地址形式的传参格式拼接
例子一: var gid=pid=pizi=sn=newsn=sn_price=city_id=123; var params = 'gid=' +123; params += '&pid=' ...
- CentOS下的Redis启动脚本
这是一个Shell脚本,用于管理Redis进程(启动,停止,重启),如果你在使用Redis,这个脚本可供参考. #!/bin/sh # # redis - this script starts and ...
- Android Shader渲染器:BitmapShader图片渲染
public class BitmapShader extends Shader BitmapShader, Shader家族的 专门处理图片渲染的 构造方法: public BitmapShade ...
- 基类View
尽管类视图看上去类的种类繁多,但每个类都是各司其职的,且从类的命名就可以很容易地看出这个类的功能.大致可分为如下三个大的功能块,分别由三个类提供对应的方法: 处理 HTTP 请求.根据 HTTP 请求 ...
- GBDT算法简述
提升决策树GBDT 梯度提升决策树算法是近年来被提及较多的一个算法,这主要得益于其算法的性能,以及该算法在各类数据挖掘以及机器学习比赛中的卓越表现,有很多人对GBDT算法进行了开源代码的开发,比较火的 ...