其他分享
首页 > 其他分享> > 1002 A+B for Polynomials (25分)

1002 A+B for Polynomials (25分)

作者:互联网

1.多项式为0,直接输出0

2.系数为0的项不输出

3.注意double类型的判0

  1 #include <iostream>
  2 #include <vector>
  3 #include <iomanip>
  4 #include <cmath>
  5 using namespace std;
  6 
  7 int main()
  8 {
  9     vector<int> a, b;//次数
 10     vector<double>a1, b1;//系数
 11     vector<int>sum;//次数
 12     vector<double>sum1;//系数
 13     int Ka;
 14     int fz = 1;//多项式为0标志,fz==0时不为0
 15     cin >> Ka;
 16     for (int i = 0; i < Ka; i++)
 17     {
 18         int temp;
 19         double temp2;
 20         cin >> temp;
 21         a.push_back(temp);
 22         cin >> temp2;
 23         a1.push_back(temp2);
 24     }
 25     int Kb;
 26     cin >> Kb;
 27     for (int i = 0; i < Kb; i++)
 28     {
 29         int temp;
 30         double temp2;
 31         cin >> temp;
 32         b.push_back(temp);
 33         cin >> temp2;
 34         b1.push_back(temp2);
 35         for (int j = 0; j < Ka; j++)
 36         {
 37             if (a[j] == b[i])
 38             {
 39                 b1[i] += a1[j];
 40             }
 41         }
 42     }
 43     int K = Ka > Kb ? Ka : Kb;
 44     int i, j;
 45     for (i = 0, j = 0; i < Ka || j < Kb;)
 46     {
 47         if (i < Ka && j < Kb)
 48         {
 49             if (a[i] == b[j])//a,b有同次项时先把a系数加到b系数上
 50             {
 51                 if (fabs(b1[j]) > 1e-15)
 52                 {
 53                     fz = 0;
 54                     sum.push_back(b[j]);
 55                     sum1.push_back(b1[j]);
 56                 }
 57                 i++;
 58                 j++;
 59             }
 60             else if (a[i] != b[j])
 61             {
 62                 if (a[i] > b[j])
 63                 {
 64                     if (fabs(a1[i]) > 1e-15)
 65                     {
 66                         fz = 0;
 67                         sum.push_back(a[i]);
 68                         sum1.push_back(a1[i]);
 69                     }
 70                     i++;
 71                 }
 72                 else
 73                 {
 74                     if (fabs(b1[j]) > 1e-15)
 75                     {
 76                         fz = 0;
 77                         sum.push_back(b[j]);
 78                         sum1.push_back(b1[j]);
 79                     }
 80                     j++;
 81                 }
 82             }
 83         }
 84         else if (i == Ka)
 85         {
 86             if (fabs(b1[j]) > 1e-15)
 87             {
 88                 fz = 0;
 89                 sum.push_back(b[j]);
 90                 sum1.push_back(b1[j]);
 91             }
 92             j++;
 93         }
 94         else if (j == Kb)
 95         {
 96             if (fabs(a1[i]) > 1e-15)
 97             {
 98                 fz = 0;
 99                 sum.push_back(a[i]);
100                 sum1.push_back(a1[i]);
101             }
102             i++;
103         }
104     }
105     int flag_out = 0;//标记是否第一次输出,控制输出格式
106     if (fz == 1)cout << "0";
107     else
108     {
109         cout << sum.size() << ' ';
110         for (int i = 0; i < sum.size(); i++)
111         {
112                 if (flag_out == 0)
113                 {
114                     cout << setprecision(1) << fixed << sum[i] << ' ' << sum1[i];
115                     flag_out = 1;
116                 }
117                 else
118                 {
119                     cout << setprecision(1) << fixed << ' ' << sum[i] << ' ' << sum1[i];
120                 }
121             }
122     }
123     return 0;
124 }

 

标签:25,Ka,int,back,Polynomials,++,b1,push,1002
来源: https://www.cnblogs.com/2020R/p/12834612.html