题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009

分析:简单题。相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序。注意点:多项式相乘后指数最高可达2000。

题目描述:

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

参考代码:

#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std;

#define max 1000
double input1[max + 1];
double input2[max + 1];
double result[2*max + 1];

int main()
{
	memset(input1,0,sizeof(input1));
	memset(input2,0,sizeof(input2));
	memset(result,0,sizeof(result));
	int k;
	int i,j;
	int e;
	//int temp=0; //记录最高指数
	double c;
	int count = 0; //最终输出的多项式的项数。
	cin>>k;
	for(i=0; i<k; i++)
	{
		cin>>e>>c;
		input1[e] += c;
	}
	cin>>k;
	for(i=0; i<k; i++)
	{
		cin>>e>>c;
		input2[e] += c;
	}

	for(i=0; i<=1000; i++)
	{
		for(j=0; j<=1000; j++)
		{
			result[i+j] += input1[i]*input2[j];
		}
	}
	for(i=0; i<=2000; i++) if(result[i] != 0) count++;
	cout<<count;
	for(i=2000; i >= 0; i--)
	{
		if(result[i] != 0.0) {
			cout<<" "<<i;
			cout<<fixed<<setprecision(1);
			cout<<" "<<result[i];
		}
	}
	cout<<endl;
	return 0;
}

【PAT】1009. Product of Polynomials (25)的更多相关文章

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

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

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

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

  4. PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

  5. PATA 1009. Product of Polynomials (25)

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

  6. PAT 解题报告 1009. Product of Polynomials (25)

    This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each ...

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

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

  8. PAT甲题题解-1009. Product of Polynomials (25)-多项式相乘

    多项式相乘 注意相乘结果的多项式要开两倍的大小!!! #include <iostream> #include <cstdio> #include <algorithm& ...

  9. 1009 Product of Polynomials (25)(25 point(s))

    problem This time, you are supposed to find A*B where A and B are two polynomials. Input Specificati ...

随机推荐

  1. [Storm] No data flows into bolt

    最近在HDP2.1的HBase环境中安装了一个Storm测试机器(单节点,JDK8),遇到了几个问题,记录下来. 尝试步骤 1. 使用和HBase一样HDP版本,直接安装Storm yum insta ...

  2. PC windows mobile 文件拷贝

    在windows 系统中提供 RAPI.DLL,只需将RAPI.DLL中的,函数导出就可以实现文件拷贝.

  3. notepad++

    plugin-manager 先要保证plugin-manager是最新的,然后才能下载其它的插件进行安装. compare插件,文件对比工具

  4. HTML DOM部分---事件 windows对象;

    <!--DOM操作windows对象操作 对浏览器进行操作document对象操作 对浏览器内页面文件进行操作 window.shuxing;属性调用格式window.fangfa();方法调用 ...

  5. Navicat 11使用技巧

    入门 Navicat 是一个可多重连接的数据库管理工具,它可让你连接到 MySQL.Oracle.PostgreSQL.SQLite.SQL Server 和/或 MariaDB 数据库,让管理不同类 ...

  6. oninput 属性

    在HTML5中,新增加了oninput属性,它和onchange 的不同就是立刻发生,而onchange 是在失去焦点的时候才发生 <script> function rangeChang ...

  7. MySQL中,修改表的某一字段的部分值

    语法:update 表名 set 字段名 = replace(字段名,'替换前内容','替换后的内容') where 条件. 如: 执行sql语句:update student set name = ...

  8. UITabBarController自定义二之xib

    UITabBarController自定义二之xib 新建一个xib文件 在UITabBarController的子类方法viewDidLoad方法中加载xib 1.-(void)viewDidLoa ...

  9. 【Centos7】5分钟理解防火墙firewalld

    Centos7中默认将原来的防火墙iptables升级为了firewalld,firewalld跟iptables比起来至少有两大好处: 1.firewalld可以动态修改单条规则,而不需要像ipta ...

  10. Unity3D手机斗地主游戏开发实战(01)_发牌功能实现

    园子荒废多年,闲来无事,用Unity3D来尝试做一个简单的小游戏,一方面是对最近研究的Unity3D有点总结,一方面跟广大的园友相互学习和提高.话不多说,进入正题~ 一.创建项目 1.创建Unity2 ...