题目: 1009 Product of Polynomials (25)(25 分)【多项式的a*b形式】
This time, you are supposed to find A*B where A and B are two polynomials.
【这次,你需要去计算两个多项式A*B的产生式】
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 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 Specification:
For each test case you should output the product 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 up to 1 decimal place.
【对每个测试案例你都应该输出a*b的产生式,并且以同样的格式输入样例,注意!在每行的结尾都要求不准出现“空格”.精确到小数点后1位,输出以幂次由大到小输出】
【注】:以下分析所在为代码注释,通过注释可以分析得到此题答案。
【注】:此代码是由Dev c++ 5.11 所写,运用c++语言。 其他方法例如c++,后续会另写微博给出方式。
/****************************************************************************************************************************/
#include<cstdio>
struct poly {
int exp; //数组的指数;
double cof; //数组的系数;
}poly[1001]; //定义结构体poly数组;
double res[2005]; //res[]为结果数组,用于存放两个行列式的加值结果;
int main()
{
int k,l,i,j,count;
/****************第一行多项式的输入******************/
scanf("%d",&k);
for(i=0;i<k;i++)
{
scanf("%d%lf",&poly[i].exp,&poly[i].cof);
}
/****************************************************/
/*输入第二行行列式的相关信息,并存储在结果数组res[]中*/
scanf("%d",&l);
for(i=0;i<l;i++)
{ double cof;
int exp; //之前在结构体中引入的exp和cof在主函数中,只能用于结构体的相关使用;
scanf("%d%lf",&exp,&cof); //再次需要再次声明第二个多项式的 幂次exp和系数cof;
for(j=0;j<k;j++)
{
res[exp+poly[j].exp]+=cof*poly[j].cof; //两多项式幂次进行相加运算,所得值在res[]中的位置,代表其系数;
}
}
for(i=0;i<2005;i++)
{
if(res[i]) count++; //遍历结果数组,查询共有多少个数据不为零,即存在二项式的个数 ;
}
printf("%d",count);
for(int i= 2005;i>=0;i--)
{
if(res[i]) printf(" %d %.1f",i,res[i]); //输入多项式的幂次和系数;
}
return 0 ;
}//要是能一次过,就坚持爱唱晚;
Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 3 3.6 2 6.0 1 1.6
总结:1、作为一个合格的结果数组res[],数组所存放的数据必须比两个多项式的最高幂次之和还要大;1000+1000=2000
2、第一个多项式的数据正常输入,在输入第二个多项式的数据的时候就可以边输入边计算了;
3、如pat题库中a1002 的a+b for polynomials一题,系数都是由大到小的输出的;
今日表白:其实有太多的时候,我想过放弃,想过颓废,坚持不下去了,可是每当我审视自己的时候,就发现现在的自己有多么的不堪与懦弱
喜欢一个人的感觉是很奇妙的,可是鼓起勇气去表白是一码事,能否被接受也是另一码事,正如我的笔记本上面所感悟:喜欢一个人,就要做一些自己不喜欢的事情,想要被别人中意,请你先变得了不起。
加油,包子! 爱唱晚,2018年7月4日19:31:20
望诸君共勉。