博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PTA一元多项式的乘法与加法c++版——山东科技大学
阅读量:4032 次
发布时间:2019-05-24

本文共 3436 字,大约阅读时间需要 11 分钟。

 

一道链表模板题,写下以后复习用 

#include
using namespace std;typedef struct number{ int xi; int zhi; struct number *next;} number,*linklist;linklist l1,l2;linklist l;linklist ml;//乘法链表int m,n;void init(){ l1=(number*)malloc(sizeof(number)); l1->next=NULL; cin>>n; number *a,*b; a=l1; for(int i=0; i
>b->xi>>b->zhi; b->next=NULL; a->next=b; a=b; } l2=(number*)malloc(sizeof(number)); l2->next=NULL; cin>>m; number *c,*d; c=l2; for(int i=0; i
>d->xi>>d->zhi; d->next=NULL; c->next=d; c=d; }}void print(){ number *a; a=ml->next; int flag=0,x=0; while(a!=NULL) { if(a->xi!=0) { if(x) cout<<' '; cout<<(a->xi)<<' '<<(a->zhi); flag=1; x++; } a=a->next; } if(flag==0) cout<<0<<' '<<0; cout<
next; while(a!=NULL) { if(a->xi!=0) { if(x) cout<<' '; cout<<(a->xi)<<' '<<(a->zhi); flag=1; x++; } a=a->next; } if(flag==0) cout<<0<<' '<<0;}void goadd(){ number *a1,*a2,*a,*b; a1=l1->next; a2=l2->next; l=(number*)malloc(sizeof(number)); l->next=NULL; a=l; while(a1!=NULL&&a2!=NULL) { if(a1->zhi==a2->zhi) { b=(number*)malloc(sizeof(number)); b->xi=a1->xi+a2->xi; b->zhi=a1->zhi; b->next=NULL; a->next=b; a=b; a1=a1->next; a2=a2->next; } else if(a1->zhi < a2->zhi) { b=(number*)malloc(sizeof(number)); b->xi=a2->xi; b->zhi=a2->zhi; b->next=NULL; a->next=b; a=b; a2=a2->next; } else { b=(number*)malloc(sizeof(number)); b->xi=a1->xi; b->zhi=a1->zhi; b->next=NULL; a->next=b; a=b; a1=a1->next; } } if(a1==NULL) { while(a2!=NULL) { b=(number*)malloc(sizeof(number)); b->xi=a2->xi; b->zhi=a2->zhi; b->next=NULL; a->next=b; a=b; a2=a2->next; } } if(a2==NULL) { while(a1!=NULL) { b=(number*)malloc(sizeof(number)); b->xi=a1->xi; b->zhi=a1->zhi; b->next=NULL; a->next=b; a=b; a1=a1->next; } }}void gomul(){ number *a1,*a2,*a,*b; a1=l1->next; a2=l2->next; ml=(number*)malloc(sizeof(number)); ml->next=NULL; a=ml; while(a2!=NULL) { while(a1!=NULL) { b=(number*)malloc(sizeof(number)); b->xi=(a1->xi)*(a2->xi); b->zhi=a1->zhi+a2->zhi; b->next=NULL; a->next=b; a=b; a1=a1->next; } a1=l1->next; a2=a2->next; } a=ml->next; while(a!=NULL) { b=a; while(b->next!=NULL) { if(b->next->zhi==a->zhi) { a->xi+=b->next->xi; b->next=b->next->next; } else { b=b->next; } } a=a->next; }}int main(){ init(); goadd(); gomul(); print(); return 0;}

ps:代码仅供参考,请勿抄袭

转载地址:http://ilqbi.baihongyu.com/

你可能感兴趣的文章
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>
用find命令查找最近修改过的文件
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
在android上运行native可执行程序
查看>>
Phone双模修改涉及文件列表
查看>>
android UI小知识点
查看>>
Android之TelephonyManager类的方法详解
查看>>
android raw读取超过1M文件的方法
查看>>
ubuntu下SVN服务器安装配置
查看>>
MPMoviePlayerViewController和MPMoviePlayerController的使用
查看>>
CocoaPods实践之制作篇
查看>>
[Mac]Mac 操作系统 常见技巧
查看>>
苹果Swift编程语言入门教程【中文版】
查看>>
捕鱼忍者(ninja fishing)之游戏指南+游戏攻略+游戏体验
查看>>
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>