您的位置:首页 > 其它

PAT-1009 Product of Polynomials (25)

2018-02-20 20:08 423 查看
题目大意:乘法运算。各指数分别加减,同时数值相乘。如下所示:



解题思路:知道了题目的意思,模拟。注意一点。对应指数的系数值经过加减后等于 0.0 应当删除。
题目链接:https://www.patest.cn/contests/pat-a-practise/1009#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;

int main(int argc, char** argv) {
double x[2] = {0,0};
map<int,double> M[3];
for(int i=0;i<2;++i)
{
int n,ex;
double co;
cin >> n;
for(int j=0;j<n;++j)
{
cin >> ex >> co;
M[i][ex] = co;
}
}
for(map<int,double>::iterator it1=M[0].begin();it1!=M[0].end();++it1)
{
for(map<int,double>::iterator it2=M[1].begin();it2!=M[1].end();++it2)
{
int EX = it1->first + it2->first;
double CO = it1->second * it2->second;
if(M[2].count(EX) == 0)
M[2][EX] = CO;
else
M[2][EX] += CO;
}
}
//cout << M[2].size(); 此处不可用size,有值为 0.0 的项目
int cnt = 0;
for(map<int,double>::reverse_iterator it=M[2].rbegin();it!=M[2].rend();++it)
{
if(it->second != 0.0)
++cnt;
}
cout << cnt;
for(map<int,double>::reverse_iterator it=M[2].rbegin();it!=M[2].rend();++it)
{
if(it->second != 0.0)
printf(" %d %.1f",it->first,it->second);
}
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: