1002 A+B for Polynomials (25)(25 分)提问
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 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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
代码:
#include<cstdio>
int main(){
double a[1010]={0};
int count=0;
int t=2;
while(t--){
int m;
int e;
double k;//指数,系数
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d%lf",&e,&k);//double的输入必须为 %lf 不然会出错丫
a[e]+=k;
}
}
for(int i=0;i<=1000;i++){
if(a[i]!=0)
count++;
}
printf("%d",count);//count有可能为0,这种情况后面不能有空格
for(int i=1000;i>=0;i--){
if(a[i]!=0){
printf(" %d %.1f",i,a[i]);
}
}
return 0;
}
这个题目需要注意的点:
1:不能统统变量定义都为int啊,有浮点运算的,都必须是double型。而且double的输入应该是%lf,我起初写的是%f然后错了,细节细节。。。。。
2:最后一个测试点格式错误,一定要注意,所有的多项式的系数对应相加后都为0,所以为0项多项式,所以你的代码里面这个0的输出后面不能跟空格啊,有就错了。