原题
1002 A+B for Polynomials (25分)
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:
where K is the number of nonzero terms in the polynomial, are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK <⋯<N2 <N1 ≤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
很烂的通过代码
好的代码指路:https://www.cnblogs.com/baichangfu/p/7168221.html
用数组下标代表指数,数组内元素存储系数
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<list>
#include<string>
#include<map>
#include<vector>
#include<iomanip>
using namespace std;
bool gt(const pair<int,double> &a, const pair<int,int> &b) {
return a.first>b.first;
}
int main() {
cout.setf(ios::fixed);
vector<pair<int,double>> p;
int k1,k2;
cin>>k1;
int i;
pair<int,double> tmp;
for(i=0;i<k1;i++) {
cin>>tmp.first>>tmp.second;
int flag=1,sz=p.size();
for(int j=0;j<sz;j++) {
if(tmp.first==p[j].first) {
p[j].second+=tmp.second;
flag=0;
break;
}
}
if(flag) p.push_back(tmp);
}
cin>>k2;
int zero=0;
for(i=0;i<k2;i++) {
cin>>tmp.first>>tmp.second;
for(int j=0;j<k1;j++) {
if(p[j].first==tmp.first) {
// cout<<"?_3"<<endl;
p[j].second+=tmp.second;
if(p[j].second==0) zero++;
// cout<<"tmp.first"<<' '<<"tmp.second"<<endl;
// cout<<tmp.first<<' '<<tmp.second<<endl;
// cout<<"p[j].first"<<' '<<"p[j].second"<<endl;
// cout<<p[j].first<<' '<<p[j].second<<endl;
break;
} else if(j==k1-1) {
p.push_back(tmp);
}
}
}
sort(p.begin(), p.end(),gt);
cout<<p.size()-zero;
for(i=0; i<p.size()-1;i++) {
if(p[i].second==0) continue;
else cout<<' '<<p[i].first<<' '<<setprecision(1)<<p[i].second;
}
if(p[i].second!=0) cout<<' '<<p[i].first<<' '<<setprecision(1)<<p[i].second;
}
测试用例&需要注意
-
同一个多项式里,可能有没有合并的、指数相同的项
test(有两个指数为2的项)
3 2 2.5 2 2.4 0 3.2
2 2 -2.5 1 0.5
result
3 2 2.4 1 0.5 0 3.2
-
注意题目要求Please be accurate to 1 decimal place,需要一位小数
- 我在这一步时使用了
setprecision(1)
,但这个函需要搭配cout.setf(ios::fixed)
使用才意味着小数点后一位有效数字,否则就成了保留一位有效数字 - 精度控制语句具体看这里:setprecision、fixed、showpoint的用法总结
test
10 2 2.5 2 2.4 0 3.2 2 2.5 2 2.4 0 3.2 2 2.5 2 2.4 0 3.2 0 4
10 2 -2.5 1 0.5 2 -2.5 1 0.5 2 -2.5 1 0.5 2.5 2 2.4 0 3.2 0 4
result
3 2 10.1 1 1.5 0 20.8
- 我在这一步时使用了
-
如果结果系数全为0,也就是结果是0,那只输出项数0
test
1 0 -3.2
1 0 3.2
result
0
我的全部测试
test | result |
---|---|
2 1 2.4 0 3.2 2 2 1.5 1 0.5 | 3 2 1.5 1 2.9 0 3.2 |
3 2 2.5 1 2.4 0 3.2 2 2 1.5 1 0.5 | 3 2 4.0 1 2.9 0 3.2 |
1 0 3.2 1 0 -3.2 | 0 |
1 0 3.2 1 0 7.89 | 1 0 6.1 |
3 2 2.5 1 2.4 0 3.2 2 2 -2.5 1 0.5 | 2 1 2.9 0 3.2 |
3 2 2.5 2 2.4 0 3.2 2 2 -2.5 1 0.5 | 3 2 2.4 1 0.5 0 3.2 |
10 2 2.5 2 2.4 0 3.2 2 2.5 2 2.4 0 3.2 2 2.5 2 2.4 0 3.2 0 4 10 2 -2.5 1 0.5 2 -2.5 1 0.5 2 -2.5 1 0.5 2.5 2 2.4 0 3.2 0 4 | 3 2 10.1 1 1.5 0 20.8 |