Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

高手请看看该如何修改程序(运行基本正确)使存储空间变小,因为我不会释放链表空间。

Posted by E10614013 at 2008-07-16 01:24:48 on Problem 1012
#include<iostream>
using namespace std;

typedef struct LNode
{
	char data;
	struct LNode *next;
	struct LNode *prior;
}LNode,*Linklist;
Linklist L;

int  create_L(int n)
{
	int i;
	L=new LNode;
	L->next=L;
	L->prior=L;
	LNode *q=L;
	for(i=0;i<n;i++)
	{
		LNode *p=new LNode;
		q->next=p;
		p->prior=q;
		p->next=L;
		L->prior=p;
		q=p;
	}
	q=L->next;
	for(i=0;i<n/2;i++)
	{
		q->data='g';
		q=q->next;
	}
	for(i=n/2;i<n;i++)
	{
		q->data='b';
		q=q->next;
	}
	return 0;
}

int main()
{
	int k,i,j,m;
	LNode *p,*q;
	int flag;
	long a[4]={93313,459901,1358657,2504881};// 由于10之后的结果是太大了,只有这样
	cin>>k;
	while(k>0&&k<14)
	{
		if(k>0&&k<10)
		{
			flag=0;
			for(i=k+1;;i=i+k+1)
			{
				create_L(2*k);
				p=L->next;
				for(m=0;m<k;)
				{
					for(j=0;j<i-1;j++)
					{
						if(p==L)
							j--;
						p=p->next;
					}
					
					if(p->data=='b')
					{
						q=p->prior;
						q->next=p->next;
						p->next->prior=q;
						delete p;
						p=q->next;
						m++;
					}
					else
						break;
				}
				if(m==k)
				{
					cout<<i<<endl;
					flag=1;
					break;
				}
				if(flag==0)
				{
					i++;
					create_L(2*k);
					p=L->next;
					for(m=0;m<k;)
					{
						for(j=0;j<i-1;j++)
						{
							if(p==L)
								j--;
							p=p->next;
						}
						if(p->data=='b')
						{
							p->prior->next=p->next;
							p->next->prior=p->prior;
							p=p->next;
							m++;
						}
						else
							break;
					}
					if(m==k)
					{
						cout<<i<<endl;
						flag=1;
						break;
					}
					else
						i--;
				}
			}
		}
		if(k>9)
			cout<<a[k-10]<<endl;
		cin>>k;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator