1009 Product of Polynomials

  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 N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

  where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (,) are the exponents and coefficients, respectively. It is given that 1, 0.

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

解题思路:
  本题给出两个多项式要求计算并输出相乘后的多项式,第一行首先给出多项式A的项数K,之后跟随2K个数,即以指数 系数的方式给出A的每一项,第二行以与多项式A相同的方式给出多项式B的信息。

要求首先输出相乘后多项式的项数,之后按指数由小到大输出多项式的指数与系数

  模拟运算过程,以三个double型的数组A、B、C记录给出的多项式与答案。

  按要求输入多项式A,在输入多项式B时每输入一项便与A的每一项相乘,答案加入数组C中,遍历C统计非零项数量并输出,按下标由大到小输出C的非零项即可。

#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6+;
//指数最大为1000,多项式相乘后的最大情况无非1000*1000
double A[MAX], B[MAX], C[MAX];
//A、B为给定多项式,C记录答案
int main()
{
int k, max_eA = INT_MIN, max_eB = INT_MIN;
scanf("%d", &k);
while(k--){ //输入多项式A
int en; //指数
double cn; //系数
scanf("%d%lf", &en, &cn);
A[en] = cn;
max_eA = max(max_eA, en); //记录多项式A的最大指数
}
scanf("%d", &k);
while(k--){ //输入多项式B
int en; //指数
double cn; //系数
scanf("%d%lf", &en, &cn);
B[en] = cn;
for(int i = max_eA; i >= ; i--)
C[i + en] += A[i] * B[en];//将该多项式B的项与多项式A的所有项对应相乘
max_eB = max(max_eB, en); //记录多项式B的最大指数
}
int cnt = ; //cnt记录答案C的非零项数
for(int i = max_eA + max_eB; i >= ; i--)
if(C[i] != 0.0)
cnt++;
printf("%d", cnt); //输出答案项数
for(int i = max_eA + max_eB; i >= ; i--) //输出答案多项式C
if(C[i] != 0.0)
printf(" %d %.1f", i, C[i]);
return ;
}

  若第一个测试点(测试点0)不通过,多半是double类型运算时误差的锅(输入A、B完成后再两个for循环运算就可能会出现这种问题)。



PTA (Advanced Level) 1009 Product of Polynomials的更多相关文章

  1. PAT (Advanced Level) 1009. Product of Polynomials (25)

    简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...

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

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

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

  6. pat 甲级 1009. Product of Polynomials (25)

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

  7. PATA 1009. Product of Polynomials (25)

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

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

  9. PTA(Advanced Level)1036.Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

随机推荐

  1. excel函数累加求和与累计百分比应用

    申明:为了方便记忆,该笔记内容纯属拷贝,如与原创雷同,请加我为火山小视频好友:345270311,必将献上好段子已表感谢~ 正传 以下表为例,求公里数的累加求和以及累计百分比. 在D2单元格输入=su ...

  2. Android webview 退出关闭声音 网页调用javascript

    关闭声音,目前没有好的办法,可以参考网络上的实用webview.reload(); @Override protected void onResume() { // TODO Auto-generat ...

  3. hdu 4882 比赛罚时贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=4882 就是CF的比赛,根据时间的推迟会相应的扣掉题目的分数,每个任务有e,k,e表示完成需要时间,k表示完成后消 ...

  4. ASP.NET Web API 框架研究 Controller创建 HttpController介绍

    对请求进行路由解析以及消息处理管道进行处理后,最后可以从HttpRequestMessage对象的属性字典中获取解析的路由数据,后边我们就可以根据其进行HttpController的创建,从前边几篇可 ...

  5. Windows 7 系统中开启 ASP.NET State Service 服务的方法

    控制面板 -> 程序和功能 -> “打开或者关闭 Windows 功能”对话框 -> Internet 信息服务 -> 万维网服务 -> 应用程序开发功能 -> A ...

  6. Sql Server Report 导出到EXCEL 指定行高

    在SQL SERVER REPORT 2005做报表的时候,发现在report中指定的行高没有用.google了一下,找到了解决方法. Make both CanGrow and CanShrink ...

  7. 绿色版Mysql自动建立my.ini和命令行启动并动态指定datadir路径

    1.先去下载绿色版的Mysql(https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.20-winx64.zip) 2.解压缩到任意目录(如D:\My ...

  8. 「雅礼集训 2017 Day1」 解题报告

    「雅礼集训 2017 Day1」市场 挺神仙的一题.涉及区间加.区间除.区间最小值和区间和.虽然标算就是暴力,但是复杂度是有保证的. 我们知道如果线段树上的一个结点,\(max=min\) 或者 \( ...

  9. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

  10. ajax请求报语法错误

    今天改代码修正完一个ajax请求后,调试发现出错进error方法,查看错误信息报语法错误,具体是调用parseJSON方法时出错,因为我是用json方式传递的参数,所以第一时间查看data参数是否正确 ...