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 ...
随机推荐
- 关于java中异常机制
什么是异常:异常就是程序在运行时出现的不正常情况.对于严重的情况Java通过Error类进行描述,一般不用编写代码处理:对于不严重的情况Java通过Exception描述,一般编写针对性代码对其进行处 ...
- 制定RPM包和加入YUM源
##################################################### ##如有转载,请务必保留本文链接及版权信息 ##欢迎广大运维同仁一起交流linux/unix ...
- 本地已经存在的项目如何跟github发生关联
切换到本地项目地址 git init 初始化项目.该步骤会创建一个 .git文件夹是附属于该仓库的工作树. git add . git commit -am 'initial commit' git ...
- 2018年江西理工大学C语言程序设计竞赛(高级组) 三角平方数
题目描述 三角数:形如图a,圆点摆放成等边三角形的数字,则为三角数. (图a) 平方数:形如图b,小方块摆放成正方形的数字,则为平方数. (图b) 那么如果一个数字既是三角形数又是平方数,则称为三角平 ...
- WPF实现QQ群文件列表动画(二)
上篇(WPF实现QQ群文件列表动画(一))介绍了WPF实现QQ群文件列表动画的大致思路,结合我之前讲过的WPF里ItemsControl的分组实现,实现起来问题不大,以下是效果图: 其实就是个List ...
- as API一些容易忘记的属性和方法
1.在flash动画里的一些动态文本会随着动画的执行,有抖动,解决问题的方法: tt为动画里的动态文本,tt.transform.matrix=null;
- 4 Template层-CSRF
1.csrf 全称Cross Site Request Forgery,跨站请求伪造 某些恶意网站上包含链接.表单按钮或者JavaScript,它们会利用登录过的用户在浏览器中的认证信息试图在你的网站 ...
- Python虚拟机类机制之instance对象(六)
instance对象中的__dict__ 在Python虚拟机类机制之从class对象到instance对象(五)这一章中最后的属性访问算法中,我们看到“a.__dict__”这样的形式. # 首先寻 ...
- asp.net实现调用ffmpeg实现视频格式的转换
视频格式转换的函数 //视频转换 public void VideoConvertFlv(string FromName, string ExportName) { string ffmpeg = H ...
- 【Linked List Cycle II】cpp
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...