This time, you are supposed to find A+B where A and B are two polynomials.
Input
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
For each test case you should output the sum 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 to 1 decimal place.
Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 2 1.5 1 2.9 0 3.2
题目大意:多项式相加;
解题思路:用set容器存储系数不为0的指数。
知识点:
1.set定义:
set<int,less<int> > setIntA; //该容器是按升序方式排列元素。
set<int,greater<int> > setIntB; //该容器是按降序方式排列元素。
2.set遍历
auto定义迭代器
for(auto it = s.begin(); it!= s.end();it++)
cout<<*it;
3.复习一下set常用知识
set.find(elem); //查找elem元素,返回指向elem元素的迭代器。
set.count(elem); //返回容器中值为elem的元素个数。对set来说,要么是0,要么是1。
set.lower_bound(elem); //返回第一个>=elem元素的迭代器。
set.upper_bound(elem); // 返回第一个>elem元素的迭代器。
set.erase(elem); //删除容器中值为elem的元素。
set.begin(); //返回容器中第一个数据的迭代器。
set.end(); //返回容器中最后一个数据之后的迭代器。
set.rbegin(); //返回容器中倒数第一个元素的迭代器。
set.rend(); //返回容器中倒数最后一个元素的后面的迭代器。
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main(){
std::vector<float> coe(1005,0);
int k, tempexp;
float tempcoe;
set<int, greater<int>>s;
for(int j = 0; j < 2; ++ j){
scanf("%d",&k);
for(int i = 0; i < k; ++ i){
scanf("%d %f",&tempexp, &tempcoe);
coe[tempexp] += tempcoe;
if(coe[tempexp] != 0)
s.insert(tempexp);
else
s.erase(tempexp);
}
}
printf("%d",s.size());
for(auto it = s.begin(); it != s.end(); ++ it)
printf(" %d %.1f",*it, coe[*it] );
}