zoukankan      html  css  js  c++  java
  • HDU1022栈模拟

    #include<iostream>
    #include<cstring>
    using namespace std;
    int main(void)
    {
        int  stack[20],len,in[10],out[20],count[20];
        int *pp=NULL,*p,top,*q,i,n;
        char strin[10],strout[10];
        while(cin>>n)
        {
            cin>>strin>>strout;
            len=strlen(strin);
            for(i=0;i<len;i++)
            {
                in[i]=strin[i]-'0';
                out[i]=strout[i]-'0';
            }
            memset(stack,0,sizeof(stack));
            memset(count,-1,sizeof(count));
            pp=&in[len-1];
            p=in;
            q=out;
            i=0;
            top=-1;
        //    cout<<*p<<' '<<*q;
            while(p<=pp)
            {
                stack[top+1]=*p;
                top++;
                count[i++]=1;
           
                while(stack[top]==*q)
                {
                    stack[top--]=0;
                    count[i++]=0;
                    q++;
                    if(top==-1)
                        break;
                }
                    p++;
            }
            if(top==-1)
            {
                cout<<"Yes."<<endl;
                for(int k=0;k<i;k++)
                {
                    if(count[k]==1)
                        cout<<"in"<<endl;
                    else if(count[k]==0)
                        cout<<"out"<<endl;
                }
                cout<<"FINISH"<<endl;
            }
            else
                cout<<"No."<<endl<<"FINISH"<<endl;
           strcpy(strin,"0");strcpy(strout,"0");
        }
        return 0;
    }

    很粗糙的栈,也搞了半天,哎~~

    一运行就爆内存错误,卡,,,吃完饭后才发现  * pp=&in[len-1];

    这里多了一个*号,,小心使得万年船啊。。。改了半天,之后还改反了:把 while(cin>>n)改成了

    while(scanf("%d %s %s"),&n,strin,strout)缺少了终止条件,怪不得outpu time limits…………

  • 相关阅读:
    打印杨辉三角C语言小程序
    奇怪的比赛蓝桥杯
    (转)Silverlight CoreCLR结构浅析
    试一试!
    (转)使用自定义行为扩展 WCF
    分组合并的使用
    多进程,多线程(转载)
    在 ASP.NET 网页中不经过回发而实现客户端回调(需要实现ICallbackEventHandler接口)
    读书笔记
    WCF学习笔记3(客户端内部运行机制分析)
  • 原文地址:https://www.cnblogs.com/cchun/p/2520064.html
Copyright © 2011-2022 走看看