hdu 1702

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1702

题意:告诉你FIFO或FILO(先进先出、先进后出),然后对每次操作进行模拟,输出结果。

mark:栈和队列的模拟。。。

代码:

# include <stdio.h>


int n ;
int dp[1010] ;


void Queue()
{
int front = 0, rear = 0, num ;
char str[20] ;
while (n--)
{
gets (str) ;
if (str[0] == 'I'){
sscanf (str+3, "%d", &num) ;
dp[rear++] = num ;
}
else
{
if (front == rear) puts ("None") ;
else printf ("%d\n", dp[front++]) ;
}
}
}


void Stack()
{
int num, top = 0 ;
char str[20] ;
while (n--)
{
gets (str) ;
if (str[0] == 'I')
{
sscanf (str+3, "%d", &num) ;
dp[top++] = num ;
}
else{
if (top == 0) puts ("None") ;
else printf ("%d\n", dp[--top]) ;
}
}
}


int main ()
{
int T ;
char ss[10] ;
scanf ("%d%*c", &T) ;
while (T--)
{
scanf ("%d %s%*c", &n, ss) ;
if (ss[2] == 'F') Queue() ;
else Stack() ;
}
return 0 ;
}



原文地址:https://www.cnblogs.com/lzsz1212/p/2336373.html