UVa 514 Rails(stack)

又犯了输出错误,从现在起,以后没提交一道题之前,都必须要再审一遍输出

根据先进后出,可以推断,如果有一个这样的图形,r[i] > r[i-1] && r[i] < r[i-2] 那么就一定不符合

代码如下:

#include <cstdio>
#include <cstring>
using namespace std;

int n, rail[1010];

int main()
{
    while ( scanf("%d", &n) != EOF && n ) {
        while ( scanf("%d", &rail[0]), rail[0] ) {
            bool is = true, now = true;
            for ( int i = 1; i < n; ++i ) scanf("%d", &rail[i]);
            for ( int i = 1; i < n; ++i ) 
                if ( rail[i] < rail[i-1] ) now = 0;
                else if ( now == 0 && ( rail[i] > rail[i-1] && rail[i] < rail[i-2] ) ) { is = false; break; }
                else if ( now == 0 && ( rail[i] > rail[i-1] && rail[i] > rail[i-2] ) ) now = true;
            if ( is ) printf("Yes\n");
            else printf("No\n"); 
        }
        printf("\n");
    }
}


你可能感兴趣的:(UVa 514 Rails(stack))