ACboy needs your help again!(栈和队列的简单应用或者数组模拟)HDU-1702

ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can’t image how dark the room he was put into is, so poor ?.
As a smart ACMer, you want to get ACboy out of the monster’s labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can’t solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem’s first line is a integer N(the number of commands), and a word “FIFO” or “FILO”.(you are very happy because you know “FIFO” stands for “First In First Out”, and “FILO” means “First In Last Out”).
and the following N lines, each line is “IN M” or “OUT”, (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
Input
The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
Output
For each command “OUT”, you should output a integer depend on the word is “FIFO” or “FILO”, or a word “None” if you don’t have any integer.
Sample Input

4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT

Sample Output

1
2
2
1
1
2
None
2
3

题意分析:FIFO表示先进先出(队列的模拟),FILO表示先进后出(栈的模拟)
解题思路:这题可以用栈和队列来写,同时还可以用数组来模拟。以下是我的两种方法。
法一:栈和队列AC代码如下

#include<stdio.h>
#include<queue>
#include<stack>
using namespace std;
char s1[10],s2[10];
int n;
void stac()//此处的函数名要注意别写成了stack,不然编译过不了
{
	stack<int> s;
	int a;
	while(n--)
	{
		scanf("%s",s2);
		if(s2[0]=='I')
		{
			scanf("%d",&a);
			s.push(a);
		}
		else if(s.empty())//如果栈为空是真
			printf("None\n");
		else
		{
			printf("%d\n",s.top());//输出栈顶元素
			s.pop();//弹出栈顶元素
		}
	}
}
void queu()//此处函数名同理
{
	queue<int> q;
	int a;
	while(n--)
	{
		scanf("%s",s2);
		if(s2[0]=='I')
		{
			scanf("%d",&a);
			q.push(a);
		}
		else if(q.empty())//如果队列为空是真
			printf("None\n");
		else
		{
			printf("%d\n",q.front());//输出队首元素
			q.pop();//将队首元素弹出
		}
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%s",&n,s1);
		if(s1[2]=='F')
			queu();
		else
			stac();
	}
	return 0;
}

法二:数组模拟法(这个代码理解需要你自己去模拟,我是一遍遍模拟之后写出来的),AC代码如下

#include<stdio.h>
int main()
{
	int a[2000],i,l,k,T,n,b;
	char s1[10],s2[10];
	scanf("%d",&T);
	while(T--)
	{
		i=l=k=0;
		scanf("%d%s",&n,s1);
		if(s1[2]=='F')
		{
			while(n--)
			{
				scanf("%s",s2);
				if(s2[0]=='I')
				{
					scanf("%d",&b);
					a[i++]=b;
					k++;
				}	
				else
				{
					k--;
					if(k<0)
					{
						printf("None\n");
						i=k=l=0;
					}	
					else
						printf("%d\n",a[l++]);
				}
			}
		}
		else
		{
			while(n--)
			{
				scanf("%s",s2);
				if(s2[0]=='I')
				{
					scanf("%d",&b);
					a[i++]=b;
					k++;
					l++;
				}	
				else
				{
					k--;
					i--;
					if(k<0)
					{
						printf("None\n");
						i=k=l=0;
					}	
					else
						printf("%d\n",a[--l]);
				}
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的课程作业管理系,源码+数据库+开题报告+论文答辩+毕业论文+视频演示 随着科学技术的飞速发展,社会的方方面面、各行各业都在努力与现代的先进技术接轨,通过科技手段来提高自身的优势,课程作业管理系统当然也不能排除在外。课程作业管理系统是以实际运用为开发背景,运用软件工程原理和开发方法,采用springboot框架构建的一个管理系统。整个开发过程首先对软件系统进行需求分析,得出系统的主要功能。接着对系统进行总体设计和详细设计。总体设计主要包括系统功能设计、系统总体结构设计、系统数据结构设计和系统安全设计等;详细设计主要包括系统数据库访问的实现,主要功能模块的具体实现,模块实现关键代码等。最后对系统进行功能测试,并对测试结果进行分析总结,得出系统中存在的不足及需要改进的地方,为以后的系统维护提供了方便,同时也为今后开发类似系统提供了借鉴和帮助。这种个性化的网上管理系统特别注重交互协调与管理的相互配合,激发了管理人员的创造性与主动性,对课程作业管理系统而言非常有利。 本课程作业管理系统采用的数据库是Mysql,使用springboot框架开发。在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。 1、关于课程作业管理系统的基本要求: (1)功能要求:可以管理首页、个人中心、公告信息管理、班级管理、学生管理、教师管理、课程类型管理、课程信息管理、学生选课管理、作业布置管理、作业提交管理、作业评分管理、课程评价管理、课程资源管理等功能模块。 (2)性能:在不同操作系统上均能无差错实现在不同类型的用户登入相应界面后能不出差错、方便地进行预期操作。 (3)安全与保密要求:用户都必须通过注册、登录才能进入系统,并且用户的权限也需要根据用户的类型进行限定。 (4)环境要求:支持多种平台,可在Windows系列、Vista系统等多种操作系统下使用。 关键词:课程作业管理系统,springboot框架; Mysql数据库 Java技术
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值