zoukankan      html  css  js  c++  java
  • poj 1363

    这是一道数据结构的问题,用到了栈的知识。题目大意讲的是每一次有N辆车从A到B,但是要将车辆的顺序重新排列,可以通过中转站C来辅助排列,但是C符合先进后出的原则,这一点和栈的特性相同。

    整个重新排序的过程其实有三种操作,A->C,C->B,A->C->B。其中A->C和C->B表示排序中需要用到栈的特性的操作,A->C->B表示该车在A中的顺序和在B中的顺序相同,可以直接沿用无需借助栈的特性。

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    int n,t[1010],s[1010];
    while(scanf("%d",&n)!=0&&n!=0)
    {
    while(scanf("%d",&t[1])!=0&&t[1]!=0)
    {
    memset(s,0,sizeof(s));
    int i,f=1;
    int a=1,b=1,top=0;
    for(i=2;i<=n;i++)
    scanf("%d",&t[i]);
    while(b<=n)
    {
    if(a==t[b])
    {
    a++,b++;
    }
    else if(top!=0&&s[top]==t[b])
    {
    s[top]=0;
    top--;
    b++;
    }
    else if(a<=n)
    {
    top++;
    s[top]=a;
    a++;
    }
    else
    {
    f=0;
    break;
    }
    }
    if(f)
    printf("Yes ");
    else
    printf("No ");
    }
    printf(" ");
    }
    return 0;
    }

  • 相关阅读:
    mongodb
    python中读取文件的read、readline、readlines方法区别
    uva 129 Krypton Factor
    hdu 4734
    hdu 5182 PM2.5
    hdu 5179 beautiful number
    hdu 5178 pairs
    hdu 5176 The Experience of Love
    hdu 5175 Misaki's Kiss again
    hdu 5174 Ferries Wheel
  • 原文地址:https://www.cnblogs.com/hbutACMER/p/4209004.html
Copyright © 2011-2022 走看看