您的位置:首页 > 其它

1009. Product of Polynomials (25)

2015-11-21 21:01 316 查看
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 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.
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

-----------------------华丽的分割线-------------------------

分析:感觉输入量不是很大,就用数组来表示多项式以及存结果了。

代码:

#include<cstdio>
#include<cstdlib>

#define OutputMaxn 2001
#define InputMaxn 1001

double input[2][InputMaxn];

double result[OutputMaxn];

int main(void)
{
int K,i,j,exp;
double coef;
for(j=0;j<2;++j)
{
scanf("%d",&K);
for(i=0;i<K;++i)
{
scanf("%d %lf",&exp,&coef);
input[j][exp] = coef;
}
}

for(i=0;i<InputMaxn;++i)
for(j=0;j<InputMaxn;++j)
{
result[i+j] += input[0][i] * input[1][j];
}

int count = 0;
for(i=0;i<OutputMaxn;++i)
{
if(result[i] != 0.0)
++count;
}
printf("%d ",count);
if(count > 0)
{
for(i=OutputMaxn - 1;i>=0;--i)
{
if(result[i] != 0.0)
{
printf("%d %.1f",i,result[i]);
--count;
if(count!=0)
printf(" ");
}
}
}
else
{
printf("0 0.0");
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: