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 ...
随机推荐
- 使用Gradle发布Android开源项目到JCenter
喜欢做些开源项目的朋友,相信有不少人都希望能把自己的项目发布到公共的中央仓库,如maven中央仓库,以供别人方便地集成使用.而使用了Android Studio的同学,应该也对gradle和jcent ...
- JNI设置C++与java的结合(2)
我们可以看到其中有四个函数声明, Java_完整类名_方法名, 完整类名包括了包名, 例如demo.Sample1是完整类名, 对应的这里就是demo_Sample1. 在注释中我们可以看到这样一个东 ...
- 【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...
- vim多行增加缩进
http://blog.163.com/clevertanglei900@126/blog/static/11135225920116891750734/ 在Normal Mode下,命令>&g ...
- C/C++创建多级目录
常常需要在非MFC的环境下创建目录,尤其是多级目录,这里写了一个创建多级目录的子函数CreateDir,以后需要就可以直接拿来用了. #include <string> #include ...
- 基于Bresenham和DDA算法画线段
直线:y=kx+b 为了将他在显示屏上显示出来,我们需要为相应的点赋值,那么考虑到计算机的乘法执行效率,我们肯定不会选择用Y=kx+b这个表达式求值,然后进行画线段. 我们应当是将它转化为加法运算. ...
- obj-c编程02:给类自动合成存取方法
我们在此篇对obj-c编程01中的Box的例子稍加改动,一是添加的自动合成存取器,二是将Box按照其标准的写法分成3个文件,即头文件Box.h,类实现文件Box.m,以及主文件test.m. 1.Bo ...
- SVN中更改连接用户
Eclipse中安装了SVN插件,当连接到SVN服务器后,便无法从客户端更改连接帐号 百度一下,也就知道 查看Eclipse中使用的是什么SVN Interface,位置在 windows > ...
- IoC和DI的基本概念的思维导图
最近在学习Spring开发,IoC这个概念让我有点儿迷糊,控制反转这四个字是在是无法做到望文生义,于是乎就找了一些材料来学习,研究了半天,绘制了下面这幅思维导图.仅供参考!
- MySQL中遇到的几种报错及其解决方法
MySQL中遇到的几种报错及其解决方法 1.[Err] 1064 - You have an error in your SQL syntax; check the manual that corre ...