PAT A1002 A+B for Polynomials

Posted mrdragon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT A1002 A+B for Polynomials相关的知识,希望对你有一定的参考价值。

PAT A1002 A+B for Polynomials

题目描述:

  This time, you are supposed to find A+B where A and B are two polynomials.

 

  Input Specification:
  Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
  K N?1?? a?N?1???? N?2?? a?N?2???? ... N?K?? a?N?K????
  where K is the number of nonzero terms in the polynomial, N?i?? and a?N?i???? (i=1,2,?,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N?K??<?<N?2??<N?1??≤1000.

 

  Output Specification:
  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

 

参考代码:

 1 /****************************************************
 2 PAT A1002 A+B for Polynomials
 3 ****************************************************/
 4 #include <iostream>
 5 #include <iomanip>
 6 #include <cmath>
 7 #include <vector>
 8 
 9 using namespace std;
10 
11 struct term 
12     double exponent = 0;
13     double coefficient = 0;
14 ;
15 
16 int main() 
17     int K1 = 0, K2 = 0;
18     double tempExp = 0, tempCoe = 0;
19 
20     cin >> K1;
21     vector<term> func1(K1);
22     for (int i = 0; i < K1; ++i) 
23         cin >> tempExp >> tempCoe;
24         func1[i].exponent = tempExp;
25         func1[i].coefficient = tempCoe;
26     
27 
28     cin >> K2;
29     vector<term> func2(K2);
30     for (int i = 0; i < K2; ++i) 
31         cin >> tempExp >> tempCoe;
32         func2[i].exponent = tempExp;
33         func2[i].coefficient = tempCoe;
34     
35 
36     vector<term> func3(K1 + K2);
37     int i = 0, j = 0, k = 0;
38     for (; i < func1.size() && j < func2.size(); ) 
39         if (func1[i].exponent == func2[j].exponent) 
40             func3[k].exponent = func1[i].exponent;
41             func3[k++].coefficient = func1[i++].coefficient + func2[j++].coefficient;
42         
43         else if (func1[i].exponent > func2[j].exponent) 
44             func3[k].exponent = func1[i].exponent;
45             func3[k++].coefficient = func1[i++].coefficient;
46         
47         else 
48             func3[k].exponent = func2[j].exponent;
49             func3[k++].coefficient = func2[j++].coefficient;
50         
51 
52         if (func3[k - 1].coefficient == 0 || fabs(func3[k - 1].coefficient) < 0.1) 
53             func3.erase(func3.begin() + k - 1);
54             --k;
55         
56     
57     func3.resize(k);
58     func3.insert(func3.end(), func1.begin() + i, func1.end());
59     func3.insert(func3.end(), func2.begin() + j, func2.end());
60 
61     func3.size() == 0 ? cout << 0 : cout << func3.size() <<  ;
62     for (int i = 0; i < func3.size(); ++i) 
63         cout << setiosflags(ios::fixed) << setprecision(0) << func3[i].exponent <<  ;
64         cout << setiosflags(ios::fixed) << setprecision(1) << func3[i].coefficient;
65         if (i != func3.size() - 1) cout <<  ;
66     
67 
68     return 0;
69 

 

注意事项:

   1:用空间换取时间也是一种减少代码运行时间的方法。

以上是关于PAT A1002 A+B for Polynomials的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1002 A+B for Polynomials

PAT-甲级-1001-A+B for Polynomials

PAT 甲级 1002 A+B for Polynomials

PAT1002 A+B for Polynomials

PAT A+B for Polynomials[简单]

PAT1002:A+B for Polynomials