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

来源:互联网 发布:黑客帝国矩阵革命 bt 编辑:程序博客网 时间:2024/06/04 18:23

题目来源: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.22 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;    }      }}


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