1002. A+B for Polynomials (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
This time, you are supposed to find A+B where A and B are two polynomials.
Input
Each input filecontainsone test case. Each case occupies 2lines, andeachlinecontainsthe information ofa polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is thenumberof nonzero terms inthe 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 thesumof A and B inoneline, withthe same formatasthe input. Notice that there must be NO extra spaceattheendofeachline. Pleasebeaccurateto1decimalplace.
Sample Input
212.403.2221.510.5
Sample Output
321.512.903.2
问题代码
#include<iostream>#include<cstdio>#include<algorithm>#include<vector>#include<iomanip>usingnamespacestd;
bool cmp(int a,int b){
return a>b;
}
int main(int argc, char *argv[])
{
ios_base::sync_with_stdio(false);
double num[1001]={0};
bool visited[1001] = {false};
int k1;
cin >> k1;
int base;double eco;
vector<int> v;
while (k1--) {
cin >> base >> eco;
num[base] += eco;
if (!visited[base]) {
v.push_back(base);
visited[base] = true;
}
}
cin >> k1;
while (k1--) {
cin >> base >> eco;
num[base] += eco;
if (!visited[base]) {
v.push_back(base);
visited[base] = true;
}
}
cout<< v.size();
sort(v.begin(),v.end(),cmp);
for (int i = 0; i < v.size(); ++i) {
cout<<" "<< v[i] << " " << fixed << setprecision(1) <<num[v[i]];
}
cout << endl;
return0;
}