[pta]02-线性结构4 Pop Sequence

    xiaoxiao2021-03-25  58

    02-线性结构4 Pop Sequence (25分)

    Given a stack which can keep MMM numbers at most. Push NNN numbers in the order of 1, 2, 3, …, NNN and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if MMM is 5 and NNN is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4. Input Specification:

    Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): MMM (the maximum capacity of the stack), NNN (the length of push sequence), and KKK (the number of pop sequences to be checked). Then KKK lines follow, each contains a pop sequence of NNN numbers. All the numbers in a line are separated by a space. Output Specification:

    For each pop sequence, print in one line “YES” if it is indeed a possible pop sequence of the stack, or “NO” if not.

    Sample Input:

    5 7 5 1 2 3 4 5 6 7 3 2 1 7 5 6 4 7 6 5 4 3 2 1 5 6 4 3 7 2 1 1 7 6 5 4 3 2

    Sample Output:

    YES NO NO YES NO


    #include <stdio.h> #include <stdlib.h> int main(){ int stackmax,n,k; scanf("%d",&stackmax); scanf("%d",&n); scanf("%d",&k); //printf("max:%d\t",stackmax); // printf("n:%d\t",n); // printf("k:%d\n",k); int arr[k][n]; int q = k; while(q!=0){ int t = n ; while(t!=0){ // printf("%d:",n-t); scanf("%d",&arr[k-q][n-t]); t--; } q--; } q = k; while(q!=0){ int t=1; //system("pause"); int max = arr[k-q][0]; int okpop=1; int stacklen=1; while(t!=n){ if(arr[k-q][t]>max){ max = arr[k-q][t]; stacklen=1; }else if(arr[k-q][t]>arr[k-q][t-1]){ okpop =0; printf("NO\n"); break; }else{ stacklen++; } //printf("max:%d\ta[%d]:%d",max,t,arr[k-q][t]); //system("pause"); t++; } if(stacklen>stackmax){ printf("NO\n"); okpop = 0; } if(okpop)printf("YES\n"); q--; } // system("pause"); return 0; } ... prompt'''

    阅读中的错误和反思

    在理解题目中的m时理解为最多建立的堆栈数量,其实是 堆栈的长度的最值;

    其中的一个测试

    达到最大size后又溢出 不是很理解,到底是什么的最大size,什么溢出,主语不知道;

    转载请注明原文地址: https://ju.6miu.com/read-39312.html

    最新回复(0)