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 ...
随机推荐
- android Gradle的几个基本概念
什么是Gradle? Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. Gr ...
- 关于hashCode与equals
首先我得说明,在我们自己写得类中你可以复写这两个方法,此时从语法的角度来说,他们没关系. 在object中 public native int hashCode(); public boolean e ...
- Linux本地网络脚本配置(内网与外网)
脚本位于: /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 //网卡的名字 BOOTPROTO=static // none静态IP ...
- 数据挖掘进阶之关联规则挖掘FP-Growth算法
数据挖掘进阶之关联规则挖掘FP-Growth算法 绪 近期在写论文方面涉及到了数据挖掘,需要通过数据挖掘方法实现软件与用户间交互模式的获取.分析与分类研究.主要涉及到关联规则与序列模式挖掘两块.关联规 ...
- Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord
Android NFC技术(三)--初次开发Android NFC你须知道NdefMessage和NdefRecord 这最近也是有好多天没写博客了,除了到处张罗着搬家之外,依旧还是许许多多的琐事阻碍 ...
- Unix - ls命令的简要实现
#include <dirent.h> 是POSIX.1标准定义的unix类目录操作的头文件,包含了许多UNIX系统服务的函数原型,例如opendir函数.readdir函数. opend ...
- 和菜鸟一起学linux之常见错误的解决和常用命令
1.错误提示:make:警告:检测到时钟错误.您的创建可能是不完整的. 解决方法:当前编译目录下,命令行输入:find . -type f -exec touch {} \; 2.SSH生成密钥:ss ...
- 项目群MSP课程最大的特点
1.课程中间让大家去了解和理解项目群管理的知识体系.方法论,更关注大家的个性化需求: 2.课程中间还会有很多练习和讨论,特别是会请到一些业界在实践MSP的客户,进行他们的实践案例分享.所以从知识到实际 ...
- myeclipse10破解
原本一直使用eclipse,但是写起web还是不太方便,由于idea使用不太顺手不太爱用.原来一直不能破解,今天迫不得已又拿起myeclipse仔细搞一番.下面是遇到的问题的总结. 不成功原因一:.原 ...
- word search(二维数组中查找单词(匹配字符串))
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...