| ||||||||||
| 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<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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator