PAT (Advanced Level) Practice 1009 Product of Polynomials(暴力循环)

题目链接Product of Polynomials
这个其实就是一个多项式的相乘,做两层for循环就可以了。
AC代码

#include <stdio.h>

int main(){
	int k1, k2, b1[11], b2[11], cnt = 0;
	double c[2002] = {0}, a1[11], a2[11];
	scanf("%d", &k1);
	for(int i = 0; i < k1; i++) scanf("%d %lf", &b1[i], &a1[i]);
	scanf("%d", &k2);
	for(int i = 0; i < k2; i++) scanf("%d %lf", &b2[i], &a2[i]);
	for(int i = 0; i < k1; i++){
		for(int j = 0; j < k2; j++){
			if(c[b1[i] + b2[j]] == 0) cnt++;
			c[b1[i] + b2[j]] += a1[i] * a2[j];
			if(c[b1[i] + b2[j]] == 0) cnt--;
		}
	}
	int max = b1[0] + b2[0];
	printf("%d", cnt);
	for(int i = max; i >= 0; i--){
		if(c[i]) printf(" %d %.1lf", i, c[i]);
	}
	return 0;
}

End

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页