第03次作业-栈和队列

1.学习总结

2.PTA实验作业

2.1 题目1:

7-1 jmu-字符串是否对称

2.2 设计思路(伪代码或流程图)

void InitStack(SqStack *&s); //初始化栈
void DestroyStack(SqStack *&s);//销毁栈
bool StackEmpty(SqStack *s);//是否为空
bool Push(SqStack *&s,ElemType e);//进栈
bool Pop(SqStack *&s,ElemType &e);//出栈
bool GetTop(SqStack *s,ElemType &e);//取栈顶元素

bool symmetry(ElemType str[]);//字符串是否对称

int i;
ElemType e;
SqStack *st;
初始化栈
for(i=0;str[i]!='';i++)
进栈
for(i=0;str[i]!='';i++)
{
出栈
if(str[i]!=e){
销毁栈
return false;
}
}
销毁栈
return true;
}

}

2.3 代码截图

2.4 PTA提交列表说明

程序没有出错,只是在提交时编译器选成C.

2.1 题目2:

7-3 表达式转换

2.2 设计思路(伪代码或流程图)


int isoperator(char op)
{
用switch记录
{
'+':
 '-':
 '*':
 '/':
}

int Priority(char op)
{
用switch循环记录
{
case '#':
case '+':
case '-':
case '*':
case '/':
case '('
case ')'int main()

{
char mid[21],post[21];
int len;
scanf输入一个中缀表达式
postfix(mid,post,len);记录符号,数字
for(int i=0;i<len;i++)
{
cout<<post[i];将存储在栈中的值依次输出
}
cout<<endl;
return 0;
}

2.3 代码截图

 

2.4 PTA提交列表说明。

2.1 题目2:

7-2 银行业务队列简单模拟

2.2 设计思路

2.3 代码截图

2.4 PTA提交列表说明

 

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

3.1 栈PTA排名

康黎彬

3.2 队列PTA排名

康黎彬

3.3 我的总分:2;

4. 阅读代码

栈7-4

 #include<bits/stdc++.h>
using namespace std;
int main()
{
int i=0,j=0;
char a[30],b[30],res[30],res2[30];
stack<char>s;
gets(a);
gets(b);
while(a[i])
{
if(a[i]==b[j])
        {
         res[j++]=a[i];
         i++;
         continue; 
        }
        while(!s.empty()&&s.top()==b[j])
        {
            res[j]=b[j];
            j++;
            s.pop();
        }
if(a[i]!=b[j])
{
s.push(a[i]);
i++;
}
}
while(!s.empty())
{
res[j++]=s.top();
s.pop();
}
res[j]='';
// printf("%s ",res);
if(strcmp(res,b)!=0)
    {
         printf("Are you kidding me?");
         return 0;
    }
i=0,j=0;
while(a[i])
{
if(a[i]==b[j])
        {
         res[j++]=a[i];
         i++;
         printf("1->2 ");
         continue; 
        }
        while(!s.empty()&&s.top()==b[j])
        {
            res[j]=b[j];
            j++;
            printf("3->2 ");
            s.pop();
        }
if(a[i]!=b[j])
{
printf("1->3 ");
s.push(a[i]);
i++;
}
}
while(!s.empty())
{
res[j++]=s.top();
s.pop();
printf("3->2 ");
}
return 0;
}
  • 每次转移1节车厢;
  • 处在1号轨道的车厢要么经过1-3连接道进入3号轨道(该操作记为"1->3"),要么经过两条连接轨道直接进入2号轨道(该操作记为"1->2");
  • 一旦车厢进入2号轨道,就不可以再移出该轨道;
  • 处在3号轨道的车厢,只能经过2-3连接道进入2号轨道(该操作记为"3->2");
  • 显然,任何车厢不能穿过、跨越或绕过其它车厢进行移动。

对于给定的1号停车顺序,如果经过调度能够实现2号轨道要求的顺序,则给出操作序列;如果不能,就反问用户 Are(你) you(是) kidding(凯丁) me(么)?



原文地址:https://www.cnblogs.com/k1206/p/8734821.html