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.每行中,第一個數字K為該多項式的非0項項數,接下來K組數字(N1,a1),N1表示該項的指數,a1表示該項系數
3.要求輸出形式與輸入形式相同
思路:
1.使用map存盤每一項,key為指數,value為系數,
注意:
1.輸出按指數從大到小排列
2.系數保留一位小數
3.如果系數為0,就不用輸出了,可能涉及測驗點3-6
4.如果使用cout輸出,注意setprecision會進行進位
5.如果系數全為0,只輸出0就可以了,這里可能導致測驗點6的段錯誤
6.使用map在判斷系數是否為0時要進行兩次遍歷,且不能當系數為0時使用erase操作,輸出的第一個數字不一定為map.size()
疑問:
1.指數一定是整數嗎?女神的代碼也是按照int來處理的,我在撰寫時使用的double存盤,不過提交時還是把小數位去掉了,,
ac代碼:
#include<iostream>
#include<map>
#include<iomanip>
using namespace std;
map<double,double> m;
int main() {
int a,count=0;
double b,c;
for(int i=0; i<2; i++) {
cin>>a;
while(a--) {
cin>>b>>c;
if(m.find(b)!=m.end()) {
m[b]+=c;
} else {
m[b]=c;
}
}
}
for(auto it=m.begin(); it!=m.end(); it++) {
if(it->second!=0) {
count++;
}
}
cout<<count;
for(auto it=m.rbegin(); it!=m.rend(); it++) {
if(it->second!=0) {
printf(" %.0f %.1f",it->first,it->second);
// cout<<" "<<setiosflags(ios::fixed)<<setprecision(0)<<it->first<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<it->second;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/264516.html
標籤:其他
上一篇:記錄如何成功添加apicloud中已經下架的模塊到專案中
下一篇:仿QQ計步器效果的實作
