Pop Sequence
来源:互联网 发布:软件需求分析方法 编辑:程序博客网 时间:2024/05/29 21:16
这是PAT上的一道题,用c++简单些,因为要用到堆栈(stack), 我用c语言写了个, 如果有简单的思路, 希望大家提示
#include<stdio.h>#include<stdlib.h>#define MAX 1000int a[MAX];typedef struct SNode{int *m;int top;} *Stack;void Push(Stack PtrS, int x);void Pop(Stack PtrS);Stack createStack(){Stack PtrS = (Stack)malloc(sizeof(struct SNode));PtrS->m = (int *)malloc(sizeof(int)*MAX);PtrS->top = -1;return PtrS;}int main(){int M, N, K, start, flag;scanf("%d%d%d", &M, &N, &K);Stack PtrS = createStack();// 一个堆栈while(K--)// 3 2 1 7 5 6 4{start = 0; flag = 1;for(int i = 0; i < N; i++)scanf("%d", &a[i]); // 读入一组数据//printf("test\n");PtrS->top = -1;for(int i = 0; i < N; i++){//printf("test\n");if(PtrS->m[PtrS->top] > a[i] && PtrS->top != -1){flag = 0; break;}if(PtrS->top == -1 || PtrS->m[PtrS->top] < a[i]){//printf("test\n");for(int j = start+1; j <= a[i] && PtrS->top < M-1; j++){Push(PtrS, j); start++;}}if(PtrS->top == M-1 && PtrS->m[PtrS->top] != a[i]){ flag = 0; break;}Pop(PtrS); } printf("%s\n", flag == 1 ? "YES" : "NO"); } return 0;}// 进栈void Push(Stack PtrS, int x){if(PtrS->top == MAX-1)return;PtrS->m[++PtrS->top] = x; } // 出栈void Pop(Stack PtrS){if(PtrS->top == -1)return; PtrS->top--; }
阅读全文
0 0
- Pop Sequence
- Pop Sequence
- Pop Sequence
- Pop Sequence
- Pop Sequence
- Pop Sequence
- Pop Sequence
- Pop Sequence
- Pop Sequence
- Pop Sequence
- 1051. Pop Sequence (25)
- 1051.Pop Sequence
- 1051. Pop Sequence
- pat 1051 pop sequence
- PAT_1051: Pop Sequence
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- 1051. Pop Sequence
- swift
- myeclipse中本地修改的文件总是更新不到cvs上
- 分布式拒绝服务攻击(DDOS)
- hdu 1007 quoit design(分冶求距离最近的两点)
- 广播读取短信
- Pop Sequence
- GitHub仓库爬虫开发记录
- Selenium2+Python2.0自动化测试【16】 日历控件
- HTML table 标签边框问题(隐藏表格边框、单元格边框等)
- SGU 271 Book Pile(deque)
- 滴滴出行测试开发岗面试经验总结
- spring-boot 学习笔记(2)
- 前后端分离springmvc和RESTful理解
- RabbitMQ+PHP消息队列系统