博客作业03--栈和队列

1.学习总结(2分)

2.PTA实验作业(4分)

题目1:7-1 jmu-字符串是否对称

设计思路

自定义栈结构体
初始化栈InitStack(SqStack *&s)
入栈Push(SqStack *&s,char e)
出栈char Pop(SqStack *&s)
主函数:
定义栈s并初始化
定义字符数组str[]
输入字符串到数组中
for i=0  str[i]!='\0'
字符串入栈
for j=0  str[j]!='\0'
如果str[j]与出栈元素不相等
flag=false
end
若字符串对称 输出yes
否则,输出no

代码截图

PTA提交列表说明

编译错误是编译器是c,改成c++,段错误是输入格式错误了

题目2:jmu-报数游戏

设计思路


输入m,n
for i=0 to n
if n<m
输出error
end
while 队列非空
报到m的人减1
while队列还非空
e存入队列元素
先出队,再将存入e的元素进队
flag判断空格,if 是空格
输出e
flag=0
否则,输出空格再输出e
最后出队
end

代码截图

PTA提交列表说明

题目3:银行业务队列简单模拟

设计思路

定义 n为总量,a为顾客编号,i,t=0判断窗口 
输入n 
模板类定义队列p,q
for  i=0  t  on
输入a
if是奇数,进队p 
if是偶数,进队q	
while队p或q不为空 
t++
if队列p非空 
输出p队头并入队 
if p或q非空 输出空格 
else	
if队列p非空 
输出p队头并入队
if 队列q非空
输出q队头并入队
输出空格 
输出\n
end 

代码截图

PTA提交列表说明

开始用了数组,但发现有运行超时,后把数组去掉了,while里面队列q非空少了!,答案错误,纠结了一下午

3.截图本周题目集的PTA最后排名

3.1 栈PTA排名

3.2 队列PTA排名

3.3 我的总分:140

4. 阅读代码

求解迷宫问题

1.栈

#include<stdio.h>  
#include<stdlib.h>  
#include<malloc.h>  
#include<iostream>  
#define MaxSize 100  
//①、定义一个顺序栈;  
typedef struct  
{  int i;          
   int j;          
   int di;         
 } Box;        
typedef struct  
{  Box data[MaxSize];  
   int top;        
 } StType;  
//②、编写走出迷宫代码;  
bool mgpath(int xi,int yi,int xe,int ye)      
{  int i,j,k,di,find;  
   StType st;             
   st.top=-1;             
   st.top++;              
   st.data[st.top].i=xi; st.data[st.top].j=yi;  
   st.data[st.top].di=-1; mg[xi][yi]=-1;   
   while (st.top>-1)       
   {    i=st.data[st.top].i;j=st.data[st.top].j;  
    di=st.data[st.top].di;    
    if (i==xe && j==ye)   
    {   printf("迷宫路径如下:\n");  
        for (k=0;k<=st.top;k++)  
        {  printf("\t(%d,%d)",st.data[k].i,st.data[k].j);  
         if ((k+1)%5==0)      
            printf("\n");  
        }  
        printf("\n");  
        return true;          
    }  
   find=0;  
   while (di<4 && find==0)     
   {  di++;  
      switch(di)  
      {  
      case 0:i=st.data[st.top].i-1;j=st.data[st.top].j;  
             break;  
      case 1:i=st.data[st.top].i;j=st.data[st.top].j+1;  
             break;  
      case 2:i=st.data[st.top].i+1;j=st.data[st.top].j;  
             break;  
      case 3:i=st.data[st.top].i,j=st.data[st.top].j-1;  
             break;  
       }  
      if (mg[i][j]==0) find=1;   
    }  
  if (find==1)             
  {  st.data[st.top].di=di;    
     st.top++;             
     st.data[st.top].i=i; st.data[st.top].j=j;  
     st.data[st.top].di=-1;  
     mg[i][j]=-1;     
  }  
  else        
  { mg[st.data[st.top].i][st.data[st.top].j]=0;  
    st.top--;     
   }  
 }  
 return false;    
}  
    
得到迷宫路径如下:  
    (1,1) (1,2) (2,2) (3,2) (3,1)   
    (4,1) (5,1) (5,2) (5,3) (6,3)   
    (6,4) (6,5) (5,5) (4,5) (4,6)   
    (4,7) (3,7) (3,8) (4,8) (5,8)   
    (6,8) (7,8) (8,8)

2.队列

#include<stdio.h>  
#include<stdlib.h>  
#include<malloc.h>  
#include<iostream>  
#define MaxSize 100  
//①、定义一个顺序栈;  
typedef struct   
{  int i,j;             
   int pre;             
} Box;            
typedef struct  
{  Box data[MaxSize];  
   int front,rear;  
} QuType;  
//②、编写走出迷宫代码;:  
bool mgpath1(int xi,int yi,int xe,int ye)     
{  int i,j,find=0,di;  
   QuType qu;         
   qu.front=qu.rear=-1;  
   qu.rear++;  
   qu.data[qu.rear].i=xi; qu.data[qu.rear].j=yi;  
   qu.data[qu.rear].pre=-1;   
   mg[xi][yi]=-1;           
   while (qu.front!=qu.rear && !find)    
   {  qu.front++;               
    i=qu.data[qu.front].i; j=qu.data[qu.front].j;  
    if (i==xe && j==ye)       
    {  find=1; print(qu,qu.front); return true; }  
    for (di=0;di<4;di++)      
    {  switch(di)  
       {  
       case 0:i=qu.data[qu.front].i-1;   
                j=qu.data[qu.front].j;    break;  
       case 1:i=qu.data[qu.front].i;   
                j=qu.data[qu.front].j+1;  break;  
       case 2:i=qu.data[qu.front].i+1;  
                j=qu.data[qu.front].j;    break;  
       case 3:i=qu.data[qu.front].i;  
                j=qu.data[qu.front].j-1;  break;  
       }  
       if (mg[i][j]==0)  
       {  qu.rear++;          
          qu.data[qu.rear].i=i; qu.data[qu.rear].j=j;  
          qu.data[qu.rear].pre=qu.front;   
          mg[i][j]=-1;  
       }  
    }  
   }  
   return false;      
}  
得到迷宫路径如下:  
  (1,1)(2,1)(3,1)(4,1)(5,1)  
    (5,2)(5,3)(6,3)(6,4)(6,5)  
    (7,5)(8,5)(8,6)(8,7)(8,8)
posted @ 2018-04-14 21:09  琼楼玉女  阅读(191)  评论(4编辑  收藏  举报