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;
}
解题思路:知道了题目的意思,模拟。注意一点。对应指数的系数值经过加减后等于 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;
}
相关文章推荐
- 浙大PAT (Advanced Level) Practise 1009 Product of Polynomials (25)
- PAT 1009 Product of Polynomials (25)多项式乘积
- PAT 1009 Product of Polynomials (25)
- PAT 1009 Product of Polynomials (25)
- PAT (Advanced Level) Practise 1009 Product of Polynomials (25)
- PAT-Java-1009-Product of Polynomials (25)
- PAT-A1009 Product of Polynomials (25)(模拟)
- ZJU PAT 1009 Product of Polynomials
- 【PAT】甲级1009 - Product of Polynomials(多项式乘法)
- PAT 1009(甲级) Product of Polynomials
- PAT(Advanced Level) 1009 - Product of Polynomials(水题)
- PAT A1009 Product of Polynomials(25)
- 1009 Product of Polynomials (25)
- PAT(甲级)Product of Polynomials (25)
- 1009 Product of Polynomials (25)
- pat甲级1009-Product of Polynomials
- PAT 1009 Product of Polynomials
- pat甲级1009:Product of Polynomials
- pat 1009 Product of Polynomials
- [map]PAT1009 Product of Polynomials