1009. Product of Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. pat1009. Product of Polynomials (25)

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. WebService详解(二)

    WsExplorer和Tcp/Ip Monitor工具本身就存在于eclipse和MyEclipse中  使用工具的原因:  1.  使用工具可以更好的了解WebService请求的过程  2.  使 ...

  2. DBA_基本Bash语法汇总

    一.变量 1.变量命名可使用英文字母.数字和下划线,必须以英文字母开头,区分大小写. 2.每个shell都拥有自己的变量定义,彼此互不影响. 3.变量直接以等号赋值,注意等号两边不可留空,若等号右侧有 ...

  3. 服务端技术进阶(二)JBoss和tomcat的区别

    JBoss和tomcat的区别 注意JBoss和tomcat是不一样,JBoss是一个可伸缩的服务器平台,当你的EJB程序编制完成后,如果访问量增加,只要通过增加服务器硬件就可以实现多台服务器同时运算 ...

  4. PS 滤镜算法原理——高反差保留 (High Pass)

    这个特效简单来说,就是一个高通滤波器, 对图像做高斯滤波,用原图减去高斯滤波后的图,再将差值加上128. clc; clear all; close all; Image=imread('4.jpg' ...

  5. OpenCV处理视频序列的类

    代码出处,opencv2 cookbook: /*--------------------------------------------------------------------------- ...

  6. 二叉查找树之 Java的实现

    参考:http://www.cnblogs.com/skywang12345/p/3576452.html 二叉查找树简介 二叉查找树(Binary Search Tree),又被称为二叉搜索树.它是 ...

  7. 使用SecureCRT的SFTP在WINDOWS与LINUX之间传输文件(转载)

    参考文献: http://ice-k.iteye.com/blog/1068275 http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888 ...

  8. 读JVM相关的一些笔记

    1.JVM的运行模式 vm一般有两种运行模式,client和server(JDK 7 后有第三种 Tiered server,后续会涉及到). client : 启动快,内存占用少,JIT编译器生成代 ...

  9. LINQ、Lambda与委托

    首先定义个Person类: public class Person { public string Name{get;set;} //姓名 public int Age{get;set;} //年龄 ...

  10. 微信小程序弹出和隐藏遮罩层动画以及五星评分

    参考源码: http://www.see-source.com/weixinwidget/detail.html?wid=82 https://blog.csdn.net/pcaxb/article/ ...