【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

method one:

programming:

  Construct an array containing 1001 elements, take the exponent term of the input polynomial as the subscript of the array, and the coefficient term as the value of the corresponding element of the subscript

  **Note: Terms that are 0 after the coefficient terms are summed are not displayed

 

The C++ code is as follows:

 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

 

Method Two:

programming:

  Using the map container from the Standard Template Library (STL)

  About map: map is a data structure used to store <key, value> key-value pairs, key and value are in one-to-one correspondence, and in map, key is unique

            1. When storing data in the map, the ascending order of the keys is installed by default to store

map<int,float> num; //By default, the data is stored in ascending order of keys, int and float are the data types of key and value respectively 

        In this problem, the exponential term of the polynomial is used as the key, and the coefficient term is stored as the value. If the output is finally printed in descending order of the key, the reverse iterator can be used to traverse the elements in the map:

map<int,float>::reverse_iterator it; //Define the reverse iterator of the map
for(it=num.rbegin();it!=num.rend();it++){
	if(it->second!=0)
		cout<<' '<<it->first<<' '<<setiosflags(ios::fixed)<<setprecision(1)<<it->second;
}        

        where: num.rbegin() : returns a reverse iterator pointing to the first element

           num.rend() : returns a reverse iterator to the last element

              it->first : indicates the key value of the element pointed to by the it iterator in the map, equivalent to (*it).first

              it->second: Indicates the value of the element pointed to by the it iterator in the map, equivalent to (*it).second

        2. When the map is stored in the data, it is sorted in descending order of the key

map<int,float,greater<int> > num; //Store data in descending order of keys
// ^ Note the space here

        In this case, the normal iterator of map can be used when printing the exponent and coefficient terms of the polynomial:

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

      

The C++ code is as follows:

 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 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325054267&siteId=291194637