【一只蒟蒻的刷题历程】【PAT】A1009 多项式的乘积

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


题目大意:

这次,您应该找到A×B,其中A和B是两个多项式。

输入规格:

每个输入文件包含一个测试用例。 每个案例占用2行,并且每行包含一个多项式的信息:

K N 1

其中K是多项式中非零项的数量,N i和a N i(i = 1,2,⋯,K)分别是指数和系数。 假设1≤K≤10,0≤N<K <⋯N2 <N1≤1000。

输出规格:

对于每个测试用例,您应该在一行中以与输入相同的格式输出A和B的乘积。 请注意,每行末尾不得有多余的空间。 请精确到小数点后一位。

样本输入:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

样本输出:

3 3 3.6 2 6.0 1 1.6


思路:

用map好,用double测试点4 5很迷,一下一下的段错误,多提交几次看运气就可以过了。。。。但是用map就一直是对的,不会有段错误。。。还是很方便的。。

ps:
测试点0:系数为0要删除
输出小数点要一位


代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
int main() 
{
   map<int,double> m1,m2,ans;
   int k,n;
   double an;
   vector<int> v1,v2; //存
   set<int> s;  //去重,统计个数,并且排好序了,直接倒着输出就行了
   
   cin>>k;
   for(int i=0;i<k;i++)
   {
   	  cin>>n>>an;
   	  m1[n]=an;
   	  v1.push_back(n); //第一个多项式
   }
   
   cin>>k;
   for(int i=0;i<k;i++)
   {
   	  cin>>n>>an;
   	  m2[n]=an;
   	  v2.push_back(n); //第二个多项式
   }
   
   for(int i=0;i<v1.size();i++)  //多项式一
   {
   	  for(int j=0;j<v2.size();j++) //多项式2
   	  {
   	  	 ans[v1[i] + v2[j]] += m1[v1[i]] * m2[v2[j]]; //放入ans
   	  	 s.insert(v1[i] + v2[j]); //插入新的指数
   	  	 if (ans[v1[i] + v2[j]]==0){  //测试点0,系数和为0要删除
                s.erase(v1[i] + v2[j]); 
            }
	  }
   }
   
   cout<<s.size();
   
   for(auto it=s.rbegin();it!=s.rend();it++)  //倒着输出
   	     	printf(" %d %.1f",*it,ans[*it]);

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值