现在的位置: 首页 > 综合 > 正文

poj 1363 Rails

2019年09月07日 ⁄ 综合 ⁄ 共 938字 ⁄ 字号 评论关闭

栈实现火车进站,出站。注意输入输出格式。

 

#include <iostream>
using namespace std;
#define MAX 1001
#define STACK_ININ_SIZE 100
#define STACK_INCREAMT 10
typedef struct stack
{
	int stacksize;
	int *base;
	int *top;
}stack;

void InitStack(stack &s)
{
    s.base = (int *)malloc(STACK_ININ_SIZE*sizeof(int));
	s.top = s.base;
	s.stacksize = STACK_ININ_SIZE;
}

void Pop(stack &s)
{
	if (s.base == s.top)
		return;
	s.top--;
}

void Push(stack &s, int n)
{
	if (s.top-s.base>=s.stacksize)
	{
		s.base = (int *)realloc(s.base, (s.stacksize+STACK_INCREAMT)*sizeof(int));
		s.top = s.base + s.stacksize;
		s.stacksize += STACK_INCREAMT;
	}
	*s.top = n;
	s.top++;
}
int IsEmpty(stack &s)
{
	if (s.base == s.top)
		return 1;
	else
		return 0;
}
stack s;
int main()
{
    int num, i, j, flag;
	int ch[MAX];
	flag = 0;
    while (cin>>num && num)
	{
		if (flag==1)
				cout<<endl;
    	flag = 1;
		while (1)
		{
			
			cin>>ch[1];
			if (ch[1]==0)
				break;
			for (i=2; i<=num; i++)
				cin>>ch[i];
			InitStack(s);
			j = 1;
			for (i=1; i<=num; i++)
			{
				Push(s, i);
				while (!IsEmpty(s))
				{
					if (*(s.top-1)==ch[j])
					{
						Pop(s);
						j++;
					}
					else
						break;
				}
			}
			if (IsEmpty(s))
				cout<<"Yes"<<endl;
			else
				cout<<"No"<<endl;
		}
	}
	return 0;
}

 

【上篇】
【下篇】

抱歉!评论已关闭.