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 0606580102 at 2007-08-09 11:28:19 on Problem 1068
#include<iostream.h>

struct stack{
	int a[20];
	int top;
};
void pop(stack &s,int &e){if(s.top!=-1)e=s.a[s.top--];}
void push(stack &s,int e){if(s.top!=19)s.a[++s.top]=e;}

void main()
{
	int t,n,*p,count,e,i,j,k;
	stack s;
	s.top=-1;
	cin>>t;//总次数
	for(i=0;i<t;i++){
		cin>>n;//右括号个数
		p=new int[n];
		for(j=0;j<n;j++)
			cin>>p[j];
		for(j=0;j<n;j++){//输出数据个数
			if(j==0){
				for(k=0;k<p[j];k++){
					e=1;
					push(s,e);
				}
				pop(s,e);e=0;push(s,e);
				cout<<1<<' ';
				continue;
			}
			for(k=0;k<p[j]-p[j-1];k++){
				e=1;
				push(s,e);
			}
			for(count=1;;count++){
				pop(s,e);
				if(e==1)
					break;
			}
			for(k=0;k<count;k++){
				e=0;
				push(s,e);
			}
			cout<<count<<' ';
		}
		cout<<endl;
	}
}
//利用堆栈思想,当p[i]中存有n时,做n次循环把1push进栈,再pop出一个修改为零,再push进去
//下一个即p[i+1]时做相同操作,但是pop时计数,直到pop出来是1,记录次数,再把所有0push回栈

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