7月12日总结

目录

8:00打卡

10:00-12:00重温栈、队列、搜索

2:00-6:00刷题


8:00打卡

10:00-12:00重温栈、队列、搜索

栈:

1、特点:先进后出;

2、方法:用数组存储,只能在栈顶进行插入和删除操作,需要设置一个top变量指示栈顶元素在栈中的位置;

队列:

1、特点:先进后出;

2、方法:用数组存储,在队头进行删除操作,队尾进行插入操作,需要设置两个变量rear和front分别指示队尾和对头;

搜索:

1、深度优先搜索(dfs):需要用到递归,从某一个点开始,访问该点,然后从该点的未被访问的邻接点出发深度优先遍历图,直到图中所有和该点有路径相通的顶点都被访问到,若图中尚有顶点未被访问,则另选图中一个未曾被访问的顶点,重复上述步骤,直到全部顶点都被访问。即一条路走到黑。

2、广度优先搜索(bfs):需要用到队列,从某个点开始,访问该点,并将该点入队,出队,依次访问队头结点的各邻接点,如果该邻接点没有入队,则将其放入队列,访问完各邻接点后,队头元素出队,重复上述操作,直到队列为空。

2:00-6:00刷题

给出一个由1到n组成的无序数列,如果我们把这个序列看成是数字1~n出栈的顺序,并且入栈顺序是1, 2, 3, ..., n,请问操作时,栈内最多能有多少个元素?

Input

输入有多组数据。

每组数据的第一行为一个整数n代表由1到n的序列。 1<= n  <=100。

第二行为n个用空格隔开的整数,范围是[1,n]。

Output

若此序列能由进栈,出栈得到,输出所使用的最小栈容,否则输出-1

Sample Input

5
1 2 3 4 5
4
3 2 1 4
6
6 5 4 1 3 2

Sample Output

1
3
-1

 题解:循环将小于等于a[i]的自然数依次入栈,计算当前栈内有多少个元素,再将栈顶元素a[i]出栈,每次出栈之前都需要比较max与top的值,随时更新最小栈容;最后当top等于0时输出最小栈容,否则输出-1。

#include <stdio.h>
int main ()
{
    int n,i,a[110],stack[110];
    while(~scanf("%d",&n))
    {
        int max=-1;
        int top=0,k=1;
        for(i=1; i<=n; i++)
            scanf("%d",&a[i]);
        for(i=1; i<=n; i++)
        {
            while(k<=a[i])//将小于等于a[i]的自然数都入栈
            {
                stack[top++]=k;
                ++k;
            }
            if(top>max)
                max=top;
            if(a[i]==stack[top-1])//将a[i]出栈
            {
                top--;
            }
        }
        if(top==0)
            printf("%d\n",max);
        else
            printf("-1\n");
    }
    return 0;
}

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.


The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.

Input

The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, ..., N. The last line of the block contains just 0.

The last block consists of just one line containing 0.

Output

The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null'' block of the input.

Sample Input

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

Sample Output

Yes
No

Yes

题解:把 中间的station看成栈,方法与上一题差不多,只是要注意输入与输出

#include  <stdio.h>
int main()
{
    int n,a[1010],i,stack[1010];
    while(~scanf("%d",&n))
    {
        if(n==0)
            break;
        while(~scanf("%d",&a[1]))//先输入一个数,通过判断条件再输入其余数
        {
            int top=0,k=1;
            if(a[1]==0)//如果第一个数等于0,则该块结束,需输出一个空行
                {printf("\n");break;}
            else
            {
                for(i=2;i<=n;i++)
                    scanf("%d",&a[i]);//如果第一个数不为0,则接着输入其余各数
            }
            for(i=1;i<=n;i++)
            {
                while(k<=a[i])
                {
                    stack[top++]=k;
                    k++;
                }
                if(stack[top-1]==a[i])
                    top--;
            }
            if(top==0)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}

定义一个二维数组:

int maze[5][5] = {

	0, 1, 0, 0, 0,

	0, 1, 0, 1, 0,

	0, 0, 0, 0, 0,

	0, 1, 1, 1, 0,

	0, 0, 0, 1, 0,

};


它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

题解:主要用到了dfs和bfs,今天在啊哈算法上看到了类似的走迷宫的题目,所以写起来不是过分麻烦,主要问题在于怎样输出每个坐标 ,啊哈算法里面是记录了当前节点的父节点,问题就转变成了怎样找到父节点并输出来,这就用到了dfs。嗯,感觉跟念绕口令似的

#include <stdio.h>
int a[30][30];
int book[10][10];
typedef struct
{
    int x;
    int y;
    int f;
    int s;
} locat;
int rear=0,head=0;
locat q[1000];
void bfs(int tx,int ty)
{
    int i,j;
    int next[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};//四个方向
    q[rear].x=tx;
    q[rear].y=ty;
    q[rear].s=0;
    q[rear].f=0;
    rear++;//入队
    while(head<rear)
    {
        for(i=0; i<=3; i++)//循环找到该点的四个方向的点,判断是否符合条件,符合就入队
        {
            tx=q[head].x+next[i][0];
            ty=q[head].y+next[i][1];
            if(tx<0||tx>=5||ty<0||ty>=5)
                continue;
            if(book[tx][ty]==0&&a[tx][ty]==0)
            {
                q[rear].x=tx;
                q[rear].y=ty;
                q[rear].f=head;
                q[rear].s=q[head].s+1;
                rear++;
                book[tx][ty]=1;
            }
            if(tx==4&&ty==4)//终点位置
            {
                return ;
            }
        }
        head++;
    }
}
void dfs(int h)//递归找到父节点
{
    if(h==0)
    {
        printf("(%d, %d)\n",q[h].x,q[h].y);
        return ;
    }
    else dfs(q[h].f);
    printf("(%d, %d)\n",q[h].x,q[h].y);
}
int main ()
{
    int i,j;
    for(i=0; i<5; i++)
        for(j=0; j<5; j++)
            book[i][j]=0;
    for(i=0; i<5; i++)
        for(j=0; j<5; j++)
            scanf("%d",&a[i][j]);
    book[0][0]=1;
    bfs(0,0);
    dfs(rear-1);
    return 0;
}

总结:

全英文的题目一点都看不懂,我恨!!本人菜鸡一枚,如果上述描述有错误,你可尽说,我可尽改!

今天主要还是在刷题,需要把重心转变一下。

终于要迎来令人头秃的JAVA了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本项目是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。该系统主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的Java学习者,包含项目源码、数据库脚本、项目说明等,有论文参考,可以直接作为毕设使用。 后台框架采用SpringBoot,数据库使用MySQL,开发环境为JDK、IDEA、Tomcat。项目经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。 该系统的功能主要包括商品管理、订单管理、用户管理等模块。在商品管理模块中,可以添加、修改、删除商品信息;在订单管理模块中,可以查看订单详情、处理订单状态;在用户管理模块中,可以注册、登录、修改个人信息等。此外,系统还提供了数据统计功能,可以对销售数据进行统计和分析。 技术实现方面,前端采用Vue框架进行开发,后端使用SpringBoot框架搭建服务端应用。数据库采用MySQL进行数据存储和管理。整个系统通过前后端分离的方式实现,提高了系统的可维护性和可扩展性。同时,系统还采用了一些流行的技术和工具,如MyBatis、JPA等进行数据访问和操作,以及Maven进行项目管理和构建。 总之,本系统是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。系统经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值