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.
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
翻译:
这一次,你应该找到A+B,其中A和B是两个多项式。
输入规格:
每个输入文件包含一个测试用例。每个案例占用2行,每行包含一个多项式的信息:
K N1 aN1 N2 aN2 ... NK aNK
其中K是多项式中非零项的数量,NK aNK(i=1,2,⋯,K)分别是指数和系数。给出1≤K≤10,0≤NK<⋯<N2<N1≤1000.
输出规格:
对于每个测试用例,您应该在一行中输出A和B的总和,格式与输入相同。请注意,每行末尾不得有额外的空格。请精确到小数点后1位。
代码 c语言:
#include <stdio.h>
#include <stdlib.h>
#define INF -9999999
int main()
{
float a[1001];
for (int i=0;i<1001;i++)
a[i]=INF;
int n,loc,max=0,total=0;float num;
for (int j=0;j<2;j++)
{
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%d%f",&loc,&num);
//if (num==0) 输入的系数里面没有0
//exit(1);
if (max<loc)
max = loc;
if (a[loc]!=INF) //如果不为空,相加计算
{
a[loc]+=num;
if (a[loc]==0) //系数为0不输出,也不计入总数
total--;
}
else //如果为空,直接赋值
{
a[loc] = num;
total++;
}
}
}
printf("%d",total);
while(1)
{
while(a[max]==INF || a[max]==0) //不为空并且不为0输出
max--;
if(max<0) //防止max为0时进入循环后变为负数输出
exit(0);
printf(" %d %.1f",max,a[max]); //1 decimal place 1位小数
max--;
}
}