1009 Product of Polynomials (25 分) PAT甲级1009 模拟 多项式相乘 巧用STL 我的代码+别人的代码

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 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​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤N​K​​<⋯<N​2​​<N​1​​≤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

题意:多项式相乘

 我的思路:用stl里的map,声明3个map<int,double>容器,遍历容器,用第一个容器里的每一项去乘第二个容器的每一项,得到的结果存到C容器里。但是要注意:多项式的系数可能为0,所以要在C容器里去掉系数为0的项。(第一个测试点卡这个)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
	int i,j,n,m;
	int r=0;
	double index;
	map<int,double> a,b;
	map<int,double,greater<int> >c;  //结果容器按键值从大到小排列 
	cin>>n;
	while(n--)
	{
		scanf("%d %lf",&r,&index);
		a.insert(make_pair(r,index));
	}
	cin>>m;
	while(m--)
	{
		scanf("%d %lf",&r,&index);
		b.insert(make_pair(r,index));
	}
	for(map<int,double>::iterator it=a.begin();it!=a.end();it++)    // 遍历 
	{
		for(map<int,double>::iterator id=b.begin();id!=b.end();id++)
		{
			int temp=it->first+id->first;
			double t=it->second*id->second;
			if(c.count(temp)!=0)
			{
				c[temp]+=t;
				if(c[temp]==0)     // 如果系数为0,去掉。第一个测试点卡这个 
				c.erase(temp);
			}
			
			else
			c.insert(make_pair(temp,t));
		}
	}
	
	if(c.size()!=0)
	{
		cout<<c.size();
		for(map<int,double>::iterator ir=c.begin();ir!=c.end();ir++)
		{
			if(ir->second!=0)
			printf(" %d %.1lf",ir->first,ir->second);
		}
		cout<<endl;
	}
	return 0;
}

别人的代码:

用浮点型数组存多项式,数组下标是指数,对应的值的系数。

暴力有时也是一种方法

#include<vector>
#include <sstream>
#include<cmath>
#include<iomanip>
#include<iostream>
#include <ctype.h>
#include <stdlib.h>
#include <algorithm>
 
using namespace std;
 
int main()
{	
	int n, m;
	float numa[1001] = {0.0};//存放A中的各项信息,下标是指数,数组的值是对应的系数
	float numb[1001] = {0.0};
	float product[2005] = {0.0};//存入乘积的结果
 
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int temp;
		cin >> temp;
		cin >> numa[temp];
	}
 
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		int temp;
		cin >> temp;
		cin >> numb[temp];
	}
	
	for (int i = 0; i <1001; i++)//i,j的取值尤其注意,一开始没考虑周全,导致两个测试用例出错
	{		 
		for (int j = 0; j <1001; j++)
		{
			float coeff = numa[i] * numb[j];
			int exp = i + j;
			product[exp] = product[exp]+ coeff;//注意这里的逻辑不要写错了
		}
	}
 
	int count = 0;
	float t = 0.05;
	for (int i = 0; i < 2005; i++)//特别注意取值范围
	{
		if (abs(product[i])>=t)//这里要特别注意,四舍五入后为0的项,也不能显示,并且要加绝对值才能通过最后一个测试用例,但是我其实不是很明白,他们都是大于0的数,为何要加绝对值,但是不加真的就是通不过第一个测试用例,减5分
			count++;
	}
	cout << count;
	
	for (int i = 2004; i >=0; i--)//特别注意取值范围
	{
		if (abs(product[i])>= t)
		{
			cout << " " << i << " " << setiosflags(ios::fixed) << setprecision(1) << product[i];
		}			
	}
 
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值