刷题记录

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);}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值