A1009 Product of Polynomials (25分)【C语言】

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值