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
第二次优化的代码:
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
第一次碰到这个问题的时候,想到用链表的来表示多项式,由于不会熟练使用标准库,所以还自己写了链表的创建和销毁,能够解决问题,但是很复杂。
再次考虑这个问题时,就会考虑标准库的便利。用vector< pair<int,int> >来存储多项式,其中pair的第一个key存储指数,第二个key存储系数,由于题目中的输入本来就是按照系数递减的方式,这就为我们接下来进行多项式求和计算提供了方便。直接将两个多项式遍历一遍即可。
特别注意的是如果A+B=0,则输入应该是”0”而不是”1 0 0”
输出精确到一位小数还是喜欢用printf(”%0.01f”, …)的方式,表示保留一位小数,如果没有则小数位填0。
PS: C语言里面每个自带的<xxx.h>头文件在C++中都会有一个<cxxx>头文件相对应,如<stdio.h>对应<cstdio>、<stdlib.h>对应<cstdlib>等。
第一次的代码:
#include <iostream>
#include <iomanip>
using namespace std;
typedef struct node
{
int exp;
float coe;
struct node *next;
}node, *myqueue;
void inputdata(int length, myqueue &Q)
{
Q = new node;
node *pre = Q;
while (0 != length)
{
node *temp = new node;
cin >> temp->exp >> temp->coe;
temp->next = NULL;
pre->next = temp;
pre = temp;
--length;
}
}
// merge Q2 into Q1
void merge(myqueue &Q1, myqueue &Q2, int L1, int L2)
{
node* p1 = Q1->next;
node* p2 = Q2->next;
node* pre = Q1;
node* temp;
delete Q2;
while (NULL != p1 && NULL != p2)
{
if (p1->exp > p2->exp)
{
pre->next = p1;
pre = pre->next;
p1 = p1->next;
}
else
{
if (p1->exp == p2->exp)
{
p1->coe += p2->coe;
temp = p2;
p2 = p2->next;
delete temp;
if (0 != p1->coe)
{
pre->next = p1;
pre = pre->next;
p1 = p1->next;
}
else
{
temp = p1;
p1 = p1->next;
delete temp;
}
}
else
{
pre->next = p2;
p2 = p2->next;
pre = pre->next;
}
}
}
pre->next = NULL;
if (p1 != NULL)
{
pre->next = p1;
}
if (p2 != NULL)
{
pre->next = p2;
}
}
int main()
{
myqueue Q1, Q2;
int L1, L2;
int length = 0;
node *temp;
cin >> L1;
inputdata(L1, Q1);
cin >> L2;
inputdata(L2, Q2);
merge(Q1, Q2, L1, L2);
temp = Q1->next;
while (NULL != temp)
{
++length;
temp = temp->next;
}
cout << length;
if (0 != length)
{
cout << ' ';
}
temp = Q1->next;
delete Q1;
for (int i = 0; i != length; ++i)
{
cout << temp->exp << ' ';
cout << fixed << setprecision(1) << temp->coe;
if (i != length - 1)
{
cout << ' ';
}
node *pre = temp;
temp = temp->next;
delete pre;
}
// system("pause");
return 0;
}
第二次优化的代码:
// 注意输入是按照系数递减的方式输入的, 这样我们在接下来的处理中就可以省去按照系数排序的过程
#include <iostream>
#include <vector>
#include <utility>
#include <cstdio>
using namespace std;
vector< pair<int,float> > A, B, sum;
int main()
{
int K, Ni;
float Ani;
vector< pair<int,float> >::iterator aiter, biter;
cin >> K;
for (int i = 0; i != K; ++i)
{
cin >> Ni >> Ani;
A.push_back(make_pair(Ni, Ani));
}
cin >> K;
for (int i = 0; i != K; ++i)
{
cin >> Ni >> Ani;
B.push_back(make_pair(Ni, Ani));
}
aiter = A.begin();
biter = B.begin();
while (aiter != A.end() && biter != B.end())
{
if (aiter->first > biter->first)
{
sum.push_back(*aiter);
++aiter;
}
else
{
if (aiter->first < biter->first)
{
sum.push_back(*biter);
++biter;
}
else
{
if (aiter->second + biter->second != 0)
{
sum.push_back(make_pair(aiter->first, aiter->second+biter->second));
}
++aiter;
++biter;
}
}
}
if (aiter != A.end())
{
sum.insert(sum.end(), aiter, A.end());
}
if (biter != B.end())
{
sum.insert(sum.end(), biter, B.end());
}
if (sum.empty())
{
cout << "0";
}
else
{
cout << sum.size();
for (int i = 0; i != sum.size(); ++i)
{
printf(" %d %0.01f", sum[i].first, sum[i].second);
}
}
cout << endl;
return 0;
}