PATA1002 A+B for Polynomials

版权声明:版权归原作者CangyeChen所有,未经原作者允许不得转载本文内容,否则将视为侵权,转载或者引用本文内容请注明来源及原作者,对于不遵守此声明或者其他违法使用本文内容者,本人依法保留追究权等 https://blog.csdn.net/CANGYE0504/article/details/88827744

PATA1002 A+B for Polynomials

This time, you are supposed to find A+B where A and B are two polynomials.

Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 2 1.5 1 2.9 0 3.2
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
	int k;
	int m,n,sum=0;
	double t;
	double a[2001]={};
	cin>>m;
	for(int i=0;i<m;i++)
	{
		scanf("%d %lf",&k,&t);
		a[k]+=t;
	}
	cin>>n;
	for(int i=0;i<n;i++)
	{
		scanf("%d %lf",&k,&t);
		a[k]+=t;
	}
	for(int j=0;j<1999;j++)
	{
		if(a[j]!=0)
		{
			sum++;
		}
	}
	cout << sum; 
	for(int j=1999;j>=0;j--)
	{
		if(a[j]!=0)
		{
			printf(" %d %.1f",j,a[j]);
		}
	}
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/CANGYE0504/article/details/88827744