PAT刷题记录
A+B for Polynomials (25) 甲级
主要涉及数组的处理问题
#include "stdio.h"
#include "iostream"
using namespace std;
int main(){
struct node{
int a; float b; }A[1005],B[1005],C[1005];
int m,n;
int count=0,count1=0,count2=0;
int num=0;
scanf("%d",&m);
for(int i=0;i<m;i++)
scanf("%d %f",&A[i].a,&A[i].b);
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d %f",&B[i].a,&B[i].b);
while(count<m&&count1<n) {
if(A[count].a>B[count1].a)
{ num++;
C[count2++]=A[count];
count++; }
if(A[count].a<B[count1].a)
{ num++;
C[count2++]=B[count1];
count1++; }
if(A[count].a==B[count1].a)
{ num++;
C[count2].a=A[count].a;
C[count2].b=A[count].b+B[count1].b;
if(C[count2].b==0)
num--;
count2++;
count1++;
count++; } }
if(count<m)
{ while(count<m)
{num++;
C[count2]=A[count];
count2++;
count++; } }
if(count1<n)
{ while(count1<n)
{ num++;
C[count2]=B[count1];
count1++; count2++; } }
printf("%d ",num);
for(int i=0;i<count2-1;i++)
{ if(C[i].b!=0)
printf("%d %.1f ",C[i].a,C[i].b); }
if(C[count2-1].b!=0)
printf("%d %.1f",C[count2-1].a,C[count2-1].b);}