PAT (Advanced Level) Practice 1002 A+B for Polynomials 分数 25

2022-11-07,,,,

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

 

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB
 

解题:

#include<stdio.h>
#include<string.h> int main()
{
int k,i,j,l,c=0;
int temp[1001];
int flag=1;
float arr1[1001],arr2[1001],temparr[1001];
memset(arr1,0,sizeof(arr1));
memset(arr2,0,sizeof(arr2));
memset(temparr,0,sizeof(temparr));
memset(temp,1001,sizeof(temp));
scanf("%d ",&k);
for(i=0;i<k;i++)
{
scanf("%d ",&j);
for(l=0;l<k;l++){
if(temp[l]==j)
{
scanf("%f ",&temparr[j]);
arr1[j]+=temparr[j];
flag=0;
}
}
if(flag==1)
{
temp[i]=j;
scanf("%f ",&arr1[j]);
}
flag=1;
}
//printf("%d %d %.1f",k,j,arr1[1]);
scanf("%d ", &k);
for(i=0;i<k;i++)
{
scanf("%d ",&j);
for(l=0;l<k;l++){
if(temp[l]==j)
{
scanf("%f ",&temparr[j]);
arr2[j]+=temparr[j];
flag=0;
}
}
if(flag==1)
{
temp[i]=j;
scanf("%f ",&arr2[j]);
}
flag=1;
} for(i=0;i<12;i++)
{
arr1[i]=arr1[i]+arr2[i];
}
for(i=0;i<12;i++)
{
if(arr1[i]!=0)
{
c++;
}
}
printf("%d",c);
for(i=12;i>=0;i--)
{
if(arr1[i]!=0)
{
printf(" %d %.1f",i,arr1[i]);
}
}
}

 

PAT (Advanced Level) Practice 1002 A+B for Polynomials 分数 25的相关教程结束。

《PAT (Advanced Level) Practice 1002 A+B for Polynomials 分数 25.doc》

下载本文的Word格式文档,以方便收藏与打印。