Time Limit: 1000MS Memory limit: 65536K
题目描述
例如,给你一个数列:1 2 3 4
push:向栈中加入一个数,比如push 5,数列就变成1 2 3 4 5。
pop:从栈中删除最后面的数,比如 pop,数列就变成1 2 3。(数列变化,但是不输出。如果栈是空的,即不能 pop 操作,那就输出 error ,但是接下来的操作还是要继续的)。
top:找出栈最后面的数,比如 top ,你就要输出4。(如果栈中没有数的话,即不能 top 操作,那就输出 empty)。
然后,你们可以看出来了吧,其实栈就是一个先进后出(越先进去的元素越后面出来)的数据结构,很简单吧,下面要检验下你们的学习效果了。
输入
输入包含多组测试数据.
每组数据的第一行为一个整数 T(1 <= T <= 1000 ),接下来 T 行为对栈的操作。
输出
如果操作是top,那么输出最后面的数,如果栈中没有数的话,那就输出“empty”(不含引号)。
如果操作是pop且栈是空的,那么输出 “error”(不含引号)。
在每组测试数据的最后多加一次换行。
示例输入
8 push 1 push 2 push 3 push 4 top pop top pop 3 push 1 pop top
示例输出
4 3 empty
提示
来源
示例程序
STL:
#include <stack>
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int T;
char s[100];
while(cin>>T)
{
stack<int> q;
for(int i=0;i<T;i++)
{
cin>>s; //操作判断
if(strcmp(s,"push")==0)
{
int n;
cin>>n;
q.push(n);//进栈
continue;
}
else if(strcmp(s,"top")==0)
{
if(q.empty())
{
cout<<"empty"<<endl; //清空判断
}
else
{
cout<<q.top()<<endl;
}
continue;
}
else if(strcmp(s,"pop")==0)
{
if(q.empty())
{
cout<<"error"<<endl;
}
else
{
q.pop(); //删除第一个元素
}
continue;
}
}
cout<<endl;//注意要求,多空一行
}
return 0;
}
数组模拟:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
int main()
{
int T, i, n ;
int stack[1000] ;
char str[1000] ;
while(scanf("%d",&T)!=EOF)
{
int top = 0;
for(i=0; i<T; i++)
{
scanf("%s",str) ;
if(strcmp(str, "push")==0)
{
scanf("%d", &n) ;
stack[top++] = n ;
}
if(strcmp(str, "pop")==0)
{
if(top==0)
{
printf("error\n") ;
continue ;
}
else
stack[top--] ;
}
if(strcmp(str, "top")==0)
{
if(top==0)
{
printf("empty\n") ;
continue ;
}
else
printf("%d\n", stack[top-1]) ;
}
}
printf("\n") ;
}
return 0 ;
}
进制转换
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
7 2 23 12 -4 3
示例输出
111 1B -11
提示
来源
示例程序
#include <stdio.h>
int main()
{
int n, r, k, i;
char b[33];
while (scanf("%d %d", &n, &r) == 2)
{
if (n == 0)
{
printf("0\n");
continue;
}
if (n < 0)
{
printf("-");
n *= -1;
}
k = 0;
while (n)
{
b[k++] = n%r;
n /= r;
}
for (i=k-1; i>=0; i--)
{
if (b[i] > 9) b[i] += 'A'-10;
else b[i] += '0';
printf("%c", b[i]);
}
printf("\n");
}
return 0;
}
数据结构实验之队列一:排队买饭
Time Limit: 1000MS Memory limit: 65536K
题目描述
中午买饭的人特多,食堂真是太拥挤了,买个饭费劲,理工大的小孩还是很聪明的,直接奔政通超市,哈哈,确实,政通超市里面也卖饭,有好几种菜,做的比食堂好吃多了,价格也不比食堂贵,并且买菜就送豆浆,吸引了不少童鞋。所以有时吧,人还是很多的,排队是免不了的,悲剧的是超市只有两个收银窗口。
问题是这样的:开始有两队人在排队,现在咱们只研究第一队,现在我们给每个人一个编号,保证编号各不相同,排在前面的人买完饭就走了,有些人挑完饭就排在后面等待付款,还有一些人比较聪明,看到另一个队人比较少,直接离开这个队到另一个队去了。我要问的是队的总人数和某个位置上人的编号。
输入
首先输入一个整数m(m<10000),代表当前有m个人,第二行输入m个数,代表每个人的编号,第三行输入一个整数n(n<10000),代表队列变动和询问一共n次,以后n行,JOIN X表示编号为X(保证与以前的编号不同)的人加入;LEAVE Y表示第Y(Y小于当前队列长度)个位置 上的人离队 ;ASK Z(Z小于当前队列长度)表示询问第Z个位置上的人的编号;FINISH D表示有D个人买完饭离开了;LENGTH表示询问队列的长度 。保证所有数据在int 范围内.
输出
对每个询问输出相应的答案,每个答案占一行。
示例输入
31 2 36JOIN 4ASK 2LEAVE 2LENGTHFINISH 2LENGTH
示例输出
231
提示
来源
示例程序
#include <stdio.h>
#include <string.h>
void main()
{
int m , j , k , n , a[10000] = {0} , x , tail ;
char s[10] ;
scanf("%d", &m);
for(tail = 1 ; tail <= m ; tail++)
scanf("%d", &a[tail]);
x = 1 ;
scanf("%d", &n);
for(j = 0 ; j < n ; j++)
{
scanf("%s", s);
if(!strcmp(s,"JOIN"))
{
scanf("%d", &a[tail++]);
}
else if(!strcmp(s,"LEAVE"))
{
scanf("%d", &m);
for(k = m-1+x ; k < tail ; k++)
a[k] = a[k+1] ;
a[tail-1] = 0 ;
tail--;
}
else if(!strcmp(s,"ASK"))
{
scanf("%d", &m);
printf("%d\n", a[m-1+x]);
}
else if(!strcmp(s,"FINISH"))
{
scanf("%d", &m);
x = m+x ;
}
else
{
printf("%d\n", tail-x);
}
}
}
约瑟夫问题
Time Limit: 1000MS Memory limit: 65536K
题目描述
n个人进行编号,分别从1到n,排成一个圈,顺时针从1开始数到m,数到m的人被杀,剩下的人继续游戏,活到最后的一个人是胜利者。
请输出最后一个人的编号。
输入
输出
示例输入
5 3
示例输出
4
提示
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
int n,m,s=0;
cin>>n>>m;
for(int i=2;i<=n;i++)
{
s=(s+m)%i;
}
cout<<s+1<<endl;
return 0;
}
数据结构实验之栈四:括号匹配
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
sin(20+10){[}]
示例输出
yesno
提示
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int main()
{
char s[50];
int n,i,st[50],top,flag;
while(gets(s)!=NULL)
{
n=strlen(s);
flag=1;
top=0;
for(i=0;i<n;i++)
{
if(s[i]=='('||s[i]=='['||s[i]=='{')
{
st[top++]=s[i];
}
else if(s[i]==')')
{
if(top!=0&&st[top-1]=='(')
{
st[top--];
}
else
{
printf("no\n");
flag=0;
break;
}
}
else if(s[i]==']')
{
if(top!=0&&st[top-1]=='[')
{
st[top--];
}
else
{
printf("no\n");
flag=0;
break;
}
}
else if(s[i]=='}')
{
if(top!=0&&st[top-1]=='{')
{
st[top--];
}
else
{
printf("no\n");
flag=0;
break;
}
}
}
if(flag==1)
{
if(top==0)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
}
return 0;
}