PAT1009:Product of Polynomials
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.用一个vector poly存放系数乘积的结果,它的下标就是x的幂次方。两个多项式相乘后的最大次幂不会超过2000(因为Ni <= 1000)。 2.用一个countx统计不同次幂的个数,如果是第一次相加不为0,那countx加1。如果系数相加为0.那么countx就得减1, 3.输出countx,然后倒序输出poly中输出系数不为0的x幂次方就行。
代码
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
vector<double> poly(2001,0); class x
{
public:
int exp;
double co;
}; int main()
{
vector<x> fst,scd;
int k1;
cin >> k1;
for(int i = 0;i < k1;i++)
{
x tmp;
cin >> tmp.exp >> tmp.co;
fst.push_back(tmp);
}
int k2;
cin >> k2;
for(int i = 0;i < k2;i++)
{
x tmp;
cin >> tmp.exp >> tmp.co;
scd.push_back(tmp);
} int countx = 0;
for(int i = 0;i < fst.size();i++)
{
for(int j = 0;j < scd.size();j++)
{
int index = fst[i].exp + scd[j].exp;
double res = fst[i].co * scd[j].co;
if(poly[index] == 0)
countx++;
poly[index] += res;
if(poly[index] == 0)
countx--;
}
} cout << countx;
for(int i = poly.size() - 1;i >= 0;i--)
{
if(poly[i] != 0)
cout << " " << i << " " << fixed << setprecision(1) << poly[i];
}
cout << endl;
}
PAT1009:Product of Polynomials的更多相关文章
- pat1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- 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 ...
- PAT Product of Polynomials[一般]
1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...
- 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 ...
- 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;乙级1041:考试座位号;乙级1004:成绩排名
题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...
- A1009 Product of Polynomials (25)(25 分)
A1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are tw ...
随机推荐
- Linux C OSS音频编程
在linux下也可以写一个类似麦克风和喇叭这样的应用程序,只要打开/dev/dsp这个设备驱动,对该设备read读操作相当于录音,对这个设备write写操作相当于放音. 对于以下出现的一些参数我就不多 ...
- 【单片机】基于有方GPRS的智能电梯控制系统
前一篇文章<时钟及温度的显示>中所介绍的作品,是作为一个单片机新手在暑假学了一个月的单片机之后,做的第一个综合性作品,涵盖了二极管.蜂鸣器.数码管.液晶屏.按键.时钟芯片.温度传感器的控制 ...
- ORM对象关系映射之GreenDAO自定义属性转换器PropertyConverter
在使用GreenDAO定义实体的属性时候,通常来说定义的实体属性名就是对应的表的字段名.实体中属性的类型(如Long.String等)就是表的字段名类型,但是我们难免会有不一样的需求,比如实体中我定义 ...
- 点击table中的某一个td,获得这个tr的所有数据
功能: 点击table中的某一个td,获得这个tr的所有数据 效果图 <html> <head> <script> function getData2(elemen ...
- CRM客户关系管理系统(十)
第十章.kingadmin+admin+actions功能开发 10.1. django admin的action 可以自己写个函数执行批量操作 crm/admin.py 后台admin actio ...
- linux_熟悉常用Linux命令
man :任何时候你觉得对一个命令行不是很确定,都可以通过输入"man + 命令"来了解这个命令能确切是做什么的. ls :列出目录内容. pwd :在终端中显示当前工作目录的全路 ...
- 【转载】详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
实例:1,http://localhost/aaa/ (打开aaa中的index.php)结果:$_SERVER['QUERY_STRING'] = "";$_SERVER['RE ...
- sublime使用技巧之集成VI
熟悉开发工具,减少多余的操作流程有助于提高开发效率,而Sublime Text 2是sublime产品的经典版本,因此本文基于Sublime Text 2讲解sublime的使用技巧. VI的主要作用 ...
- Ocelot中文文档-委托处理程序
Ocelot允许用户将委托处理程序添加到HttpClient传输中. 这个功能在github #208中提出,我确定它会以各种方式被使用.之后我们在GitHub#264中进行了扩展. 用法 为了将委托 ...
- java之Spring(AOP)-Annotation实现添加切面
我们已经知道之前的切面添加方式(动态代理),是定义了一个实现了InvocationHandler接口的Handlerservice类,然后 在这个类内部写好切面逻辑,包括切面放置的位置,很显然下面的这 ...