PAT 甲级 1002 A+B for Polynomials

#include<stdio.h>
#define MAX 1001
//思路:用数组的下标存储指数(整数),对应的值存储系数(小数);
//		在输入过程中计算需要输出的数组元素的个数(count); 
//		最后由后向前检索不为零的数组元素并输出; 
int main()
{
	 double a[MAX]={0};
	 int count1=0,count2=0;
	 int count=0;
	 int N1,N2;
	 scanf("%d",&count1);
	 for(int i=0;i<count1;i++)//用数组记录第一行输入值 
	 {
	 	scanf("%d",&N1);
	 	scanf("%lf",&a[N1]);
	 }
	 scanf("%d",&count2);
	 count=count1+count2;//赋初值 
	 for(int i=0;i<count2;i++)//用记录第二行输入值 
	 {
	 	double temp=0;
	 	scanf("%d",&N2);
	 	scanf("%lf",&temp);
	 	if(a[N2]!=0)//判断是否出现新的数组下标,此处不是 
	 	{
	 		a[N2]+=temp;
	 		count--;//第一行出现同样的下标,无需重复输出,减一 
	 		if(a[N2]==0)
	 			count--;//计算后的值为零,无需输出,减一 
		}
		else
			a[N2]=temp;//新的数组下标 
	 }
	 printf("%d",count);//注意:若此时为零,后面不能有空格 
	 for(int i=MAX-1;i>=0;i--)//由后向前输出数组元素 
	 {
	 	if(a[i]!=0)
	 	{
	 		printf(" %d %0.1lf",i,a[i]);
	 		count--;
		}	
	 }
	 printf("\n");
	 return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42348049/article/details/80515740