PAT甲级 1002. A+B for Polynomials (25) c++链表程序

题目来源:http://www.patest.cn/contests/pat-a-practise/1002

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

Input

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

 

Output

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

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
================code======================
发现网上一般都是用数组回答,由于刚看了mooc上浙大的数据结构,有讲到用链表回答多项式乘法与加法,便一开始就想到链表。
#include<stdio.h>
#include <stdlib.h>

struct node{
  int expon;
  double coef;
  struct node *next;
}; 
typedef struct node * polynomials;

polynomials  Read();
polynomials Add(polynomials p1,polynomials p2);
int polysum(polynomials p5);
void PrintfPoly(polynomials p3);

int main(){
  polynomials p1,p2,p3;
  int sum;
  p1=Read();
  p2=Read();
  p3=Add(p1,p2);
  sum=polysum(p3);
  if(sum==0)
  printf("0\n");
  else 
  printf("%d ",sum);
  PrintfPoly(p3);
  return 0;
}

polynomials Read(){
  int n,ex=1010;
  polynomials head,temp,node;
  head=(polynomials)malloc(sizeof(polynomials));
  head->next=NULL;
  temp=head;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    node=(polynomials)malloc(sizeof(polynomials));
    scanf("%d%lf",&(node->expon),&(node->coef));
    if(node->expon!=ex){
    temp->next=node;
      node->next=NULL;
      temp=node;
      ex=node->expon;
    }
    else{
      temp->coef+=node->coef;
  }
    }
    return head;
} 

polynomials Add(polynomials p1,polynomials p2){
  polynomials p3,head,temp;
  head=(polynomials)malloc(sizeof(polynomials ));
  temp=head;
  head->next=NULL;
  p1=p1->next;
  p2=p2->next;
  while (p1 != NULL&&p2 != NULL){
  p3=(polynomials)malloc(sizeof(polynomials ));
  p3->next=NULL;
  if(p1->expon==p2->expon){
    if(p1->coef+p2->coef==0){
      p1=p1->next;
      p2=p2->next;
    }
    else{
      p3->coef=p1->coef+p2->coef;
      p3->expon=p1->expon;
      temp->next=p3;
      temp=p3;
      p1=p1->next;
      p2=p2->next;
    }
  }
  else if(p1->expon>p2->expon){
    p3->coef=p1->coef;
    p3->expon=p1->expon;
    temp->next=p3;
    temp=p3;
    p1=p1->next;
  }
  else{
    p3->coef=p2->coef;
    p3->expon=p2->expon;
    temp->next=p3;
    temp=p3;
    p2=p2->next;
  }
  }
    if (p1 == NULL){
    temp->next = p2;
  }
  else if (p2 == NULL){
    temp->next = p1;
  }
  return head;
}
int polysum(polynomials p3){
  int i=0;
  polynomials p5;
  p5=p3->next;
  while(p5){
    i++;
    p5=p5->next;
  }
  return i;
}

void PrintfPoly(polynomials p3){
  polynomials p4;
  p4=p3->next;
  if(!p4)
  {
  }
  else{
    while(p4){  
      if(p4->next==NULL)
      printf("%d %.1f\n",p4->expon,p4->coef);
      else 
          printf("%d %.1f ",p4->expon,p4->coef);
          p4=p4->next;
    }
    
  }
}


一开始是测试点三四一直不通过,后来发现有个问题所在,现在测试点全部通过。
有问题可以一起讨论。

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值