听说可爱的小盆宇都会写队列和栈
7:15~7:20 打卡学习
8:00~10:00 记单词
18:00~23:00 看大话数据结构,刷题
本以为昨天在啊哈算法上把队列和栈的知识看的差不多了,没想到今天翻开刚到的大话数据结构一看,自己之前学的其实只是皮毛罢了,于是我又花了不少时间把大话数据结构上关于队列和栈的知识仔仔细细看了一遍,虽然有些东西我还只能做到一知半解(所以今天就不细说具体学到了些啥了),但我相信随着我做的题越来越多,我掌握的知识运用的也会越来越熟练的。
今天只刷了两个题,都是水题,就不细讲了,直接上题吧。
四月一日快到了,Vayko想了个愚人的好办法——送礼物。嘿嘿,不要想的太好,这礼物可没那么简单,Vayko为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物。盒子里面可以再放零个或者多个盒子。假设放礼物的盒子里不再放其他盒子。
用()表示一个盒子,B表示礼物,Vayko想让你帮她算出愚人指数,即最少需要拆多少个盒子才能拿到礼物。
Input
本题目包含多组测试,请处理到文件结束。
每组测试包含一个长度不大于1000,只包含’(’,’)'和’B’三种字符的字符串,代表Vayko设计的礼物透视图。
你可以假设,每个透视图画的都是合法的。
Output
对于每组测试,请在一行里面输出愚人指数。
Sample Input
((((B)()))())
(B)
Sample Output
4
1
这个题我一开始想了不少时间,因为这题我压根就想不到为什么会用到栈和队列,后来发现是我想多了,的确是个水题哈哈。
代码:
#include<stdio.h>
#include<string.h>
int main()
{
char ch[1010];
while(scanf("%c",&ch[0])!=EOF)
{
int i=0,count=0;
scanf("%s",ch+1);
//printf("%s\n",ch);
getchar();
while(ch[i]!='B')
{
if(ch[i]=='(') count++;
if(ch[i]==')') count--;
i++;
}
printf("%d\n",count);
memset(ch,0,sizeof(ch));
}
}
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
这个题嘛实际上就是个“工具人”,方便我们熟练模拟队列和栈的,没啥挑战性,就是要注意注意输入和getchar()。
代码:
#include<stdio.h>
#include<string.h>
void fun1(int n)
{
//printf("OK!!!\n");
int a[1010],head=0,tail=0;
char ch[4];
while(n--)
{
getchar();
scanf("%c",&ch[0]);
if(ch[0]=='I')
{
scanf("%s ",ch+1);
scanf("%d",&a[tail++]);
memset(ch,0,sizeof(ch));
}
if(ch[0]=='O')
{
scanf("%s",ch+1);
if(tail>head)
printf("%d\n",a[head++]);
else printf("None\n");
memset(ch,0,sizeof(ch));
}
//printf("n=%d!!!\n",n);
}
}
void fun2(int n)
{
//printf("OK!!!\n");
int a[1010],top=-1;
char ch[4];
while(n--)
{
getchar();
scanf("%c",&ch[0]);
if(ch[0]=='I')
{
scanf("%s ",ch+1);
scanf("%d",&a[++top]);
memset(ch,0,sizeof(ch));
}
if(ch[0]=='O')
{
scanf("%s",ch+1);
if(top>=0)
printf("%d\n",a[top--]);
else printf("None\n");
memset(ch,0,sizeof(ch));
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
char ch[5];
scanf("%d%s",&n,ch);
//printf("n=%d ch=%s\n",n,ch);
if(strcmp(ch,"FIFO")==0) fun1(n);
if(strcmp(ch,"FILO")==0) fun2(n);
memset(ch,0,sizeof(ch));
//printf("continue!!!\n");
}
}
总计学习7小时(真不容易)。
嘿咻嘿咻,这段时间好忙呀,有点担心题刷不完了呢,把明天空出来专门刷题,明天至少刷4道题,争取明天就把题刷到达标数。