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
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 Input2 1 2.4 0 3.22 2 1.5 1 0.5Sample 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; } }}
一开始是测试点三四一直不通过,后来发现有个问题所在,现在测试点全部通过。
有问题可以一起讨论。
- 【PAT甲级】【C】1002. A+B for Polynomials (25)
- PAT甲级 1002. A+B for Polynomials (25) c++链表程序
- [PAT-甲级]1002.A+B for Polynomials
- PAT 甲级 1002. A+B for Polynomials
- PAT甲级1002. A+B for Polynomials
- PAT 甲级 1002. A+B for Polynomials
- pat甲级1002.A+B for Polynomials
- PAT甲级1002. A+B for Polynomials (25)
- PAT - 甲级 - 1002. A+B for Polynomials (25)
- PAT甲级1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25)-PAT甲级真题
- PAT甲级.1002. A+B for Polynomials (25)
- 1002. A+B for Polynomials (25) PAT 甲级
- 【PAT甲级】1002. A+B for Polynomials (25)
- PAT 甲级 1002. A+B for Polynomials (25)
- PAT甲级 1002. A+B for Polynomials (25)
- PAT TEST甲级1002. A+B for Polynomials (25)
- PAT甲级练习1002. A+B for Polynomials (25)
- 常量
- 如何解决PL/SQL连接Oracle时数据库下拉框为空?
- Java—网络编程实现UDP聊天室(局域网)
- 底部按钮 字体变颜色+焦点事件
- Linux安装vmware workstation
- PAT甲级 1002. A+B for Polynomials (25) c++链表程序
- python_list
- 自定义view之环形进度条加数字
- 将图片设置为网页的背景
- 不戴金箍如何救你,带了金箍如何愛你
- 织梦cms内核vip解析包
- java设计模式 ——观察者模式
- OKHttp + RxJava请求数据
- Fragment的切换方法