题目:1002 a+b for polynomials (25)(25 分)【多项式的a+b形式】
This time, you are supposed to find A+B where A and B are two polynomials.
【计算两个多项式A+B的和】
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.
【每个输入文件都包括一个测试例。每个例子包括两行,每行都包括一个多项式的信息:K N1 a1 N2 a2 ……Ni ai,K是多项式里面有K个项,其中1<=K<=10,幂次N最大为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.
【对每个测试案例你都应该输出A+B的和,并且以同样的格式输入样例,注意!在每行的结尾都要求不准出现“空格”.精确到小数点后1位,输出以幂次由大到小输出】
【注】:以下分析所在为代码注释,通过注释可以分析得到此题答案。
【注】:此代码是由Dev c++ 5.11 所写,运用c++语言。 其他方法例如c++,后续会另写微博给出方式。
/****************************************************************************************************************************/
#include<cstdio>
int main()
{
int m=1111,k,count=0,i,n; //由题意得,0<N<=1000,所以m必须大于1000
double a; //系数a必须是double型
double p[m]={};
scanf("%d",&k); //第一行输入具体数值 ;
for(i=0;i<k;i++)
{
scanf("%d%lf",&n,&a);
p[n]=a; //把系数A存入P[]数组 ;
}
scanf("%d",&k); //第二行输入具体数值 ;
for(i=0;i<k;i++)
{
scanf("%d%lf",&n,&a);
p[n]+=a; //幂次相同的二项式 使其系数相加;
}
for(i=0;i<m;i++)
{
if(p[i]!=0)
{
count++; //t计数,系数相加后,如果不等于零,则为确实存在的二项式,记录共有多少二项式;
}
}
printf("%d",count);
while(m--)
{
if(p[m]!=0) //输出系数不等于0的切实存在的二项式;
printf(" %d %.1f",m,p[m]); //倒序输出,输出幂次n和系数a,a保留一位小数; 注意输出格式;
}
return 0;
}//唱晚,爱你哟~ 2018年7月3日10:56:17
/******************************************************************************************************************/
测试点
2 1 2.4 0 3.2
2 2 1.5 1 0.5
测试结果
3 2 1.5 1 2.9 0 3.2