这个题我一直没发现错误,只对了第一个样例,我看了好几遍都没有发现哪里错了,最后发现float d[1001]忘了初始化定义,小伙伴们不要像我这样粗心奥。
题目:
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 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 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.
题意:每行包含一个测试案例,每个案例包含两行,每行包括一个多项式的信息:K N1 aN1 N2 aN2 ... NK aNK,k是非零的数在多项式中,A、B分别是指数与系数,这是给的1≤K≤10,0≤nK<⋯<n2<n1≤100 0,对于每个测试案例,您应该输出总和之A和B在一行中,与输入格式相同。请注意,每行末尾不得有额外的空间。请精确到1小数点的地方。
散装英语,新手上路,轻喷!!!
输入样例
2 1 2.4 0 3.2
2 2 1.5 1 0.5
输出样例
3 2 1.5 1 2.9 0 3.2
代码段
#include <iostream>
using namespace std;
int main(){
int a,b,sum=0;
float d[1001]={0},c;
for(int i=0;i<2;i++){
cin>>a;
for(int j=0;j<a;j++){
cin>>b>>c;
d[b]=d[b]+c;
}
}
for(int i=0;i<1001;i++){
if(d[i]!=0) sum++;
}
cout<<sum;
for(int i=1000;i>=0;i--){
if(d[i]!=0)
printf(" %d %.1f",i,d[i]);
}
return 0;
}