1002. A+B for Polynomials (25)

数组模拟多项加法

#include<stdio.h>
#include<math.h>
#include<string.h>

#define MAX 1000
double p[MAX+1];

int main()
{
	int aNum;
	while( scanf("%d", &aNum) != EOF )
	{
		//initial
		memset(p, 0, sizeof(p[0])*(MAX+1));
		//input a
		int e;
		double c;
		while(aNum--)
		{
			scanf("%d%lf", &e, &c);
			p[e] += c;
		}
		//input b
		int bNum;
		scanf("%d", &bNum);
		while(bNum--)
		{
			scanf("%d%lf", &e, &c);
			p[e] += c;
		}
		//output
		int cnt = 0;
		for(int i = 0; i <= MAX; ++i)
			if( fabs(p[i]) > 1e-6 ) cnt++;
		
		printf("%d", cnt);
		for(int i = MAX; i >= 0; --i)
		{
			if( fabs(p[i]) > 1e-6 )
				printf(" %d %.1lf", i, p[i]);
		}
		printf("\n");
	}
	return 0;
}


 

 

你可能感兴趣的:(pat,ZJU)