其他分享
首页 > 其他分享> > 1002 A+B for Polynomials

1002 A+B for Polynomials

作者:互联网

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:

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

审题

题目要求两个一元多项式的和。一元多项式中的项用两个整数标识,例如:2 1.5 标识1.5x^2。

思路

  1. 用数组来存储一元多项式,下标标识指数,数组元素值表示系数。

  2. 数组初始化为0,读入多项式时直接加到对应的位置即可。

参考代码

 1 #include<stdio.h>
 2 #define maxsize 10000 
 3 int main()
 4 {
 5     int i,k,j,count=0;
 6     double a;
 7     double p[maxsize];
 8     for(i=0;i<maxsize;i++){//初始化数组 
 9         p[i]=0;
10     }
11     scanf("%d",&k);//输入项数 
12     for(i=0;i<k;i++){//读入指数和系数 
13         scanf("%d %lf",&j,&a);
14         p[j]=p[j]+a;
15     }
16     scanf("%d",&k);
17     for(i=0;i<k;i++){
18         scanf("%d %lf",&j,&a);
19         p[j]=p[j]+a;
20     }
21     for(i=0;i<maxsize;i++){
22         if(p[i]!=0){
23             count++;
24         }
25     }
26     printf("%d",count);
27     for(i=maxsize-1;i>-1;i--)
28     {
29         if(p[i]!=0){
30             printf("% d %.1lf",i,p[i]);
31         }
32     }
33     return 0;
34 }

 

 



标签:case,1.5,多项式,Polynomials,标识,each,line,1002
来源: https://www.cnblogs.com/TreBienBA/p/12668042.html