寒假集训作业(3)——栈与队列

来源:互联网 发布:淘宝号怎么更换身份证 编辑:程序博客网 时间:2024/05/17 06:30

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2556

#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>using namespace std;int temp[1000];int main(){    int top;    char s[10];    int n;    int T,i;    while(cin>>T)    {        top=0;        for(i=0;i<T; i++)        {            cin>>s;            if(!strcmp(s,"push"))            {                scanf("%d", &n);                temp[top++] = n;            }            else if(!strcmp(s,"pop"))            {                if(!top) {cout<<"error"<<endl;continue;}                else temp[--top];            }            else if(!strcmp(s,"top"))            {                if(!top) {cout<<"empty"<<endl;continue;}                else cout<<temp[top-1]<<endl;            }        }        cout<<endl;    }    return 0;}

//No.2#include<stdio.h>#include<string.h>#include<stdlib.h>#define maxsize 100int top=0;int push(int stack[],int x){    if(top>maxsize)        return 0;    stack[top++]=x;    return 1;}int pop(int stack[]){    return stack[--top];}int stackempty(){    if ( top == 0)        return 1;    else        return 0;}int main(){    int n,s[maxsize],p,q;    char m[5];    while(scanf("%d",&n)==1)    {    top=0;    while(n>0)    {        scanf("%s",m);        if(strcmp(m,"push")==0)        {            scanf("%d",&q);            push(s,q);        }        if(strcmp(m,"pop")==0)        {            if(stackempty()==0)            {                top--;            }            else                printf("error\n");        }        if(strcmp(m,"top")==0)        {            if(stackempty()==0)            {                printf("%d\n",s[top-1]);            }            else                printf("empty\n");        }        n--;    }    printf("\n");    }    return 0;}


上题只是粗浅地运用了栈的思想,采用数组方式实现。要注意栈顶指针的位置。


 

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2131

#include <iostream>using namespace std;int main(){    int n,r,top=0;    int ans[10000];    cin>>n>>r;    for(;n!=0;top++)    {        ans[top]=n%r;        n/=r;    }    while(top)    {        cout<<ans[--top];    }    cout<<endl;}

也是运用了栈的思想,仍然需要注意指针的位置。


 

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2133

#include <iostream>#include <cctype>#include <cstdio>using namespace std;int calc(int a,int b,char symbol){    switch (symbol)    {    case '+':return a+b;    case '-':return a-b;    case '*':return a*b;    case '/':return a/b;    }}int main(){    char formula[1000];    int mark=0;    int temp[100];    int i=0;    while(scanf("%c",&formula[i]))    {       if(formula[i]=='#')  break;       i++;    }    int size=i;    for(int i=0;i<=size-1;i++)    {        if(isdigit(formula[i]))//come up with digit        {            temp[mark]=formula[i]-'0';            mark++;        }        else        {            temp[mark-2]=calc(temp[mark-2],temp[mark-1],formula[i]);            mark--;        }    }    cout<<temp[0];}

 

跟学姐一起改了好久,出错有几个地方:1.switch语句格式错误;2.输入到哪里截止是一个重点;3.碰到符号只需要计算符号前面的两位,然后让他们入栈;4.碰到单纯的数字就让他们入栈;

#include <iostream>#include <cstring>#include <cstdio>using namespace std;int main(){    char a[100];    char temp[100];    int mark=0;    while(cin>>a)    {        mark=0;        for(int i=0;i<strlen(a);i++)        {            if(a[i]=='('||a[i]=='['||a[i]=='{')//push in all left-brackets...               {                   temp[mark++]=a[i];//push in stack...               }            else if(a[i]==')'||a[i]==']'||a[i]=='}')//when right-brackets appears...                {                    if(a[i]==')'&&temp[mark-1]=='(')//just seek the first-left one whether matches...                    {                        mark--;//re-write the left-bracket's address(mark)...                    }                    else if(a[i]==']'&&temp[mark-1]=='[')                    {                        mark--;//re-write the left-bracket's address(mark)...                    }                    else if(a[i]=='}'&&temp[mark-1]=='{')                    {                        mark--;//re-write the left-bracket's address(mark)...                    }                    else                    {                        /*temp[mark]=a[i];//not mentioned so next...                        mark++;//move the pointers...*/                    }                }            }        if (!mark) cout<<"yes"<<endl;//empty stack means it matches perfectly...        else cout<<"no"<<endl;    }    return 0;}

You have to initialize "mark"!

 

思想是,碰到左括号,让左括号入栈,碰到右括号,让右括号从栈内搜索,如果有,让他们出栈,没有就进行下一个。。。


//No.2#include<stdio.h>#include<string.h>int main(){char s[1000];while(gets(s)){int i,j,top=0;char a[1000];int p=strlen(s);for(i=0;i<p;i++){if(s[i]=='('||s[i]=='['||s[i]=='{'){a[top]=s[i];top++;}else if(s[i]==')'||s[i]==']'||s[i]=='}'){if(a[top-1]=='('&&s[i]==')'){a[top-1]='\0';top--;}else if(a[top-1]=='['&&s[i]==']'){a[top-1]='\0';top--;}else if(a[top-1]=='{'&&s[i]=='}'){a[top-1]='\0';top--;}else{a[top]=s[i];top++;}}}if(top==0){printf("yes\n");}else{printf("no\n");}}return 0;}


原创粉丝点击