【PAT】1002. A+B for Polynomials (25)

1002. A+B for Polynomials (25)

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

方法一:

程序设计:

  构造一个包含1001个元素的数组,把输入的多项式的指数项当做数组的下标,系数项当做该下标对应元素的值

  **注意:系数项求和之后为0的项不显示

C++ 代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main(){
 5     float num[1001];
 6     for(int i=0;i<1001;i++){
 7         num[i]=0.0;
 8     }
 9     int m,n,temp1,count=0;
10     float temp2;
11     cin>>m;
12     for(int i=0;i<m;i++){
13         cin>>temp1>>temp2;
14         num[temp1]+=temp2;
15     }
16     cin>>n;
17     for(int i=0;i<n;i++){
18         cin>>temp1>>temp2;
19         num[temp1]+=temp2;
20     }
21     for(int i=0;i<1001;i++){
22         if(num[i]!=0)
23             count++;
24     }
25     cout<<count;
26     for(int i=1000;i>=0;i--){
27         if(num[i]!=0)
28             cout<<' '<<i<<' '<<setiosflags(ios::fixed)<<setprecision(1)<<num[i];
29     }
30     system("pause");
31 }
C++ Code 1

方法二:

程序设计:

  使用标准模板库(STL)中的 map 容器

  关于 map :map 是用来存放 <key,value>  键值对的数据结构,key 和 value 一一对应,并且在map 中,key是唯一的

            1.在map中存入数据时,默认安装key的升序来存放

map<int,float> num; //默认按照key的升序排列存储数据,int,float分别为key和value的数据类型 

        此题中将多项式的指数项作为key,系数项作为value进行存储,若最后按照key的降序进行打印输出时,可使用逆向迭代器来遍历map中的元素:

map<int,float>::reverse_iterator it;  //定义map的逆向迭代器 
for(it=num.rbegin();it!=num.rend();it++){
	if(it->second!=0)
		cout<<' '<<it->first<<' '<<setiosflags(ios::fixed)<<setprecision(1)<<it->second;
}        

        其中:num.rbegin() :返回指向第一个元素的逆向迭代器

           num.rend() :返回指向最后一个元素的逆向迭代器

              it->first :表示map中 it 迭代器指向的元素的key值,等同于 (*it).first

              it->second:表示map中 it 迭代器指向的元素的value值,等同于 (*it).second

        2.将map存入数据时按照key的降序排列

map<int,float,greater<int> > num;  //按照key的降序来存储数据 
//                        ^   注意此处有空格 

        这时,再打印多项式的指数项和系数项时可使用map的普通的迭代器:

map<int,float>::iterator it;  //普通的迭代器 
for(it=num.begin();it!=num.end();it++){
	if(it->second!=0)
		cout<<' '<<it->first<<' '<<setiosflags(ios::fixed)<<setprecision(1)<<it->second;
}

      

C++ 代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main(){
 5     map<int,float,greater<int> > num;    
 6     int m,exp;
 7     float coef;
 8     for(int i=0;i<2;i++){
 9         cin>>m;
10         for(int j=0;j<m;j++){
11             cin>>exp>>coef;
12             num[exp]+=coef;
13         }
14     }
15     m=0;
16     map<int,float>::iterator it;
17     for(it=num.begin();it!=num.end();it++){
18         if(it->second!=0)
19             m++;
20     }
21     cout<<m;
22     for(it=num.begin();it!=num.end();it++){
23         if(it->second!=0)
24             cout<<' '<<it->first<<' '<<setiosflags(ios::fixed)<<setprecision(1)<<it->second;
25     }
26     system("pause");
27 }
C++ Code 2 

猜你喜欢

转载自www.cnblogs.com/pgzhang/p/8968811.html