听说可爱的小盆宇都会写队列和栈

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道题,争取明天就把题刷到达标数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是关于用C语言编单调队列和单调的一些基本思路: 单调队列: 单调队列是一种能够在O(1)时间内完成以下操作的队列:在队尾添加元素、在队首删除元素、查找队列中的最小(或最大)元素。 在C语言中,可以使用数组模拟队列,并通过双端队列的思想来实现单调队列。具体实现时,我们可以使用一个数组来存储队列中的元素,并使用两个指针front和rear分别指向队首和队尾元素。为了实现单调性,我们需要维护一个递增(或递减)的队列,即队列中的元素按照一定的顺序排列。为了实现这个目标,我们可以在插入元素时,从队尾开始遍历队列,将所有比插入元素小的元素都出队,最后将插入元素入队。这样,我们就可以保证队列中的元素是单调递增的。查找最小元素时,只需要返回队首元素即可。 以下是用C语言实现单调队列的基本代码: ```c #include <stdio.h> #define MAXSIZE 1000 int queue[MAXSIZE]; // 队列 int front = 0, rear = 0; // 队首和队尾指针 // 判断队列是否为空 int isEmpty() { return front == rear; } // 判断队列是否已满 int isFull() { return rear == MAXSIZE; } // 在队尾添加元素 void enqueue(int x) { while (rear > front && queue[rear - 1] > x) { rear--; } queue[rear++] = x; } // 在队首删除元素 void dequeue() { if (!isEmpty()) { front++; } } // 查找队列中的最小元素 int getMin() { if (!isEmpty()) { return queue[front]; } return -1; } int main() { enqueue(3); enqueue(1); enqueue(5); dequeue(); printf("%d\n", getMin()); return 0; } ``` 单调: 单调是一种能够在O(1)时间内完成以下操作的:在顶添加元素、在顶删除元素、查找中的最小(或最大)元素。 与单调队列类似,我们也可以使用数组模拟,并通过的特性来实现单调。为了实现单调性,我们需要维护一个递增(或递减)的,即中的元素按照一定的顺序排列。为了实现这个目标,我们在插入元素时,从顶开始遍历,将所有比插入元素小的元素都出,最后将

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值