A1009 Product of Polynomials (25分)【C语言】
还是开了三个数组(汗-_-||, 其实只需要开一个,另一个边读入边处理就可以。
1、第一次的时候测试点45段错误,意识到,多项式相乘后,系数范围是原来的2倍,因此注意数组大小的确定。
2、一开始测试点1通不过,发现放到外面单独统计一次非零项的个数就通过了,应该是测试点1中包含系数为0的情况。
题目描述:
This time, you are supposed to find A×B where A and B are two polynomials.
输入格式:
输出格式:
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.
输入样例:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
输出样例:
3 3 3.6 2 6.0 1 1.6
实现代码:
#include <stdio.h>
#define MAXN 1001
int main()
{
int K1, K2, i, j, cnt=0, c;
double A1[MAXN]={0}, A2[MAXN]={0}, A3[MAXN*2]={0}, e;
scanf("%d", &K1);
for(i=0; i<K1; ++i){
scanf("%d %lf", &c, &e);
A1[c] = e;
}
scanf("%d", &K2);
for(i=0; i<K2; ++i){
scanf("%d %lf", &c, &e);
A2[c] = e;
}
for(i=0; i<MAXN; ++i){
for(j=0; j<MAXN; ++j){
if(A1[i]!=0 && A2[j]!=0){
A3[i+j] += A1[i] * A2[j];
}
}
}
for(i=0; i<MAXN*2; ++i){
if(A3[i]!=0) cnt++;
}
printf("%d", cnt);
for(i=MAXN*2-1; i>=0; --i){
if(A3[i]!=0){
printf(" %d %.1f", i, A3[i]);
cnt--;
}
if(cnt == 0) break;
}
return 0;
}