数据结构 02-线性结构4 Pop Sequence

刚开始拿到这道题的时候错误的做法:

1 //相邻之间的数小于等于M
 2 //首先弹出来的数小于等于M
 3 #include<stdio.h>
 4 #include<math.h>
 5 #define MAXN = 1000000;
 6 int M,N,K;
 7 int main(){
 8     int pre,now,i,j,flag=0;
 9     scanf("%d %d %d",&M,&N,&K);
10     for(i=0;i<K;i++){
11         flag = 0;
12         scanf("%d",&now);
13         if(now>M) flag=1;
14         for(j=1;j<N;j++){
15             pre = now;
16             scanf("%d",&now);
17             if(abs(pre-now)>M) flag=1;
18         }
19         if(flag==1) printf("NO\n");
20         else printf("YES\n");
21     }
22
23     return 0;
24 }

后来发现问题后,重新选择的方法:

 1 #include<stdio.h>
 2 #define MAXN 10000
 3 int M,N,K;
 4 int b[MAXN],stack[MAXN],top;
 5 int detect();
 6 int main(){
 7     int p,i,j,flag=0;
 8     scanf("%d %d %d",&M,&N,&K);
 9     b[0] = N;
10     for(i=0;i<K;i++){
11        for(j=1;j<=N;j++){
12            scanf("%d",&b[j]);
13        }
14        if(detect()==1) printf("YES\n");
15        else printf("NO\n");
16     }
17     return 0;
18 }
19 int detect(){
20     int i,j,value,flag,start;
21     top = 0;
22     stack[top] = b[0];
23     start= 1;
24
25     for(i=1;i<=b[0];i++){
26         value = b[i];
27         flag = 0;
28         for(j=1;j<=top;j++){
29             if(stack[j]==value) flag = 1;
30         }
31         //不在栈中
32         if(flag==0){
33             //前面的元素入栈
34             if(start<=value){
35                 for(j=start;j<=value;j++){
36                     ++top;
37                     if(top>M) return 0;
38                     else stack[top] = j;
39                     }
40                 start = value+1;
41                 top--;
42             }
43
44         }
45         //在栈中
46         else{
47             if(stack[top]!=value) return 0;
48             else top--;
49
50         }
51
52     }
53     return 1;
54 }

总结:使用了栈的方法,注意栈的容量问题。

原文地址:https://www.cnblogs.com/Learn-Excel/p/12610772.html

时间: 2024-07-31 07:55:28

数据结构 02-线性结构4 Pop Sequence的相关文章

线性结构4 Pop Sequence

02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., Nand pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example,

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

02-线性结构4 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example

数据结构练习 02-线性结构3. Pop Sequence (25)

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N 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 M is 5 and N is 7, we ca

数据结构1 线性结构

数据结构是指数据元素的结合及元素间的相互关系和构造方法.元素之间的相互关系是数据的逻辑结构,元素关系的存储形式成为存储结构.数据结构按照逻辑关系的不同分为线性结构和非线性结构两大类.其中线性结构是最基本的结构,元素顺序排列,常见的有线性表.栈.队列.数组.串. 一.线性表 1.线性表是最简单也是最常用的一种线性结构.一个线性表示n(n>=0)个元素的有限序列,非空线性表的特点为: 存在唯一的一个"第一个"元素: 存在唯一的一个"最后一个"元素: 除第一个元素外

02-线性结构4 Pop Sequence

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N 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 M is 5 and N is 7, we ca

数据结构之线性结构

import java.util.*; class SLType{ public static void main(String args[]){ int i; SL sl = new SL(); DATA pdata; String key; System.out.println("the show of sequence list"); sl.SLlnit(sl); System.out.println("initialise sequence list is end&q

数据结构入门-线性结构

把所有的节点用一根直线串起来 连续存储[数组] 什么叫做数组:元素类型相同,大小相等 重点看代码吧,需要注意的都在注释里,多敲几遍,当然了,有些功能还没有实现,以后再实现 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> // 定义了一个数据类型,这个数据类型的名字叫做struct Arr,有三个成员 struct Arr { int * pBase; // 存储的是数组第一个元素的地址 int le

数据结构(1) -- 线性结构

1.顺序存储 //List.h #ifndef LIST_H_ #define LIST_H_ #define MAXSIZE 100 typedef struct _Poly { int a; int n; bool operator == (_Poly e) { if (a == e.a && n == e.n) return true; else return false; } }Poly; #define ElemType Poly typedef struct _Node { E

13. C#数据结构与算法 -- 线性结构

本文中,我们讨论了三个部分的内容: 什么是线性结构,线性结构有哪些特点 . 详细介绍了一个最简单线性结构顺序表,并且通过源代码进行一些的分析. 最后还举了一个例子,让我们更好的理解顺序表. 第一部分:什么是线性结构,线性结构有哪些特点 什么是线性结构,线性结构是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这 种一对一的关系指的是数据元素之间的位置关系,即: (1)除第一个位置的数据元素外,其它数据元素