// Stack.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
const int MAX_NUM = 65535;
bool Test(int *pnBuffer, int nLen)
{
int nTemp = 0;
int nMin = 0;
bool bFirst = true;
for (int i = 1; i < nLen - 1; ++i)
{
nTemp = pnBuffer[i]; // 保存出栈序列中的值
for (int j = i + 1; j < nLen; ++j)
{
if (pnBuffer[j] < nTemp && bFirst) // 后面的数比该数小,逆序
{
nMin = pnBuffer[j]; // 取出后面的第一个数
bFirst = false;
continue;
}
if (pnBuffer[j] > nMin && pnBuffer[j] < nTemp && !bFirst)
{
return false;
}
}
bFirst = true;
}
return true;
}
BOOL SplitString(char *pcBuffer, int *pnBuffer)
{
char seps[] = " ";
char *token = NULL;
int i = 0;
token = strtok(pcBuffer, seps);
int nLen = atoi(token);
pnBuffer[0] = nLen;
while (token != NULL)
{
++i;
token = strtok( NULL, seps );
if (token != NULL)
{
pnBuffer[i] = atoi(token);
}
}
return TRUE;
}
int main(int argc, char* argv[])
{
int nIndex = 0;
char cBuffer[MAX_NUM] = {0};
int pnBuffer[MAX_NUM] = {0};
string strInput("");
printf("请输入:\r\n");
while (TRUE)
{
if (fgets(cBuffer, MAX_NUM, stdin) == NULL)
{
break;
}
SplitString(cBuffer, pnBuffer);
if (Test(pnBuffer, pnBuffer[0]))
{
printf("1\r\n");
}
else
{
printf("0\r\n");
}
memset(cBuffer, '\0', MAX_NUM);
memset(pnBuffer, '\0', MAX_NUM);
}
return 0;
}