天天看點

pat 1009. Product of Polynomials (25)

連結:http://pat.zju.edu.cn/contests/pat-a-practise/1009

題意:計算多項式的乘積。

分析:基于程式設計友善,還是采用數組的方式來實作。

#include<stdio.h>

typedef struct Poly {
	int exp;
	double coe;
} Poly;

Poly a[1005];
Poly b[1005];
double res[2005];

int main() {
	int n;
	scanf("%d", &n);
	int i;
	for (i = 0; i < n; i++) {
		scanf("%d%lf", &a[i].exp, &a[i].coe);
	}
	int m;
	scanf("%d", &m);
	for (i = 0; i < m; i++) {
		scanf("%d%lf", &b[i].exp, &b[i].coe);
	}
	int j;
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++) {
			res[a[i].exp + b[j].exp] += a[i].coe * b[j].coe;
		}

	int count = 0;
	for (i = 0; i < 2005; i++) {
		if (res[i]) {
			count++;
		}

	}

	printf("%d", count);

	for (i = 2005; i >= 0; i--) {
		if (res[i])
			printf(" %d %.1lf", i, res[i]);
	}

	printf("\n");

	return 0;
}