Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
头次碰到超内存。贴个代码,用链表做的。各位大牛有什么方法能改进吗#include<stdio.h> #include<stdlib.h> typedef struct node { int date; struct node *next; }link; link *Jian(int n); //建立链表并返回头结点,头结点也包含数据 link *End(link *head,int n); //寻找尾结点,方便查找 int main(void) { int n,k,j,m,z,q; link *head,*end; while(scanf("%d",&n)&&n!=0) { j=k=2*n; z=m=n+1; head=Jian(2*n); end=End(head,n); while(k!=n) { while(--m) { end=end->next; head=head->next; } m=z; head=head->next; q=end->next->date; //数据从1到n free(end->next); //释放结点 end->next=head; if(q<=n&&q>=1) { if(!m%n) //跳过前n个数据开始找 { m=m+n; z=m; } z=++m; k=j; head=Jian(2*n); end=End(head,n); } else { k--; } } printf("%d\n",m); } return 0; } link *Jian(int n) { int i=0; link *head,*p1,*p2; p1=head=(link *)malloc(sizeof(link)); while(--n) { p1->date=++i; p2=(link *)malloc(sizeof(link)); p1->next=p2; p1=p2; } p1->date=++i; p1->next=head; return head; } link *End(link *head,int n) { link *end=(link *)malloc(sizeof(link)); int j=1; while(j<2*n) { head=head->next; j++; } end=head; return end; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator