数据结构 栈&队列
2017-12-21 16:32
351 查看
7-2 符号配对(20 分) 请编写程序检查C语言源程序中下列符号是否配对:/*与*/、(与)、[与]、{与}。 输入格式: 输入为一个C语言源程序。当读到某一行中只有一个句点.和一个回车的时候,标志着输入结束。程序中需要检查配对的符号不超过100个。 输出格式: 首先,如果所有符号配对正确,则在第一行中输出YES,否则输出NO。然后在第二行中指出第一个不配对的符号:如果缺少左符号,则输出?-右符号;如果缺少右符号,则输出左符号-?。 输入样例1: void test() { int i, A[10]; for (i=0; i<10; i++) /*/ A[i] = i; } . 输出样例1: NO /*-? 输入样例2: void test() { int i, A[10]; for (i=0; i<10; i++) /**/ A[i] = i; }] . 输出样例2: NO ?-] 输入样例3: void test() { int i double A[10]; for (i=0; i<10; i++) /**/ A[i] = 0.1*i; } . 输出样例3: YES -------------------------------------------------------------------- #include<cstdio> #include<iostream> #include<cmath> #include<malloc.h> #include<stdlib.h> #include<string.h> #include<cstring> #define STACK_INIT_SIZE 10000 #define STACKINCREMENT 10 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 using namespace std; typedef char SElemType,Status; typedef struct { SElemType *base; SElemType *top; int stacksize; } Stack; Status InitStack(Stack &S) { S.base=(SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE); if(!S.base) exit(OVERFLOW); S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK; } Status Push(Stack &S,SElemType e) { if(S.top-S.base>=S.stacksize) { S.base=(SElemType*)malloc(sizeof(SElemType)*(S.stacksize+STACKINCREMENT)); if(!S.base) exit(OVERFLOW); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; } Status Pop(Stack &S) { if(S.top==S.base) return ERROR; S.top--; return OK; } Status GetTop(Stack &S,SElemType &e) { if(S.base==S.top) return ERROR; e=*(S.top-1); return OK; } const int maxn=1000+5; char s[maxn]; bool Find(Stack &S,char ch) { char tmp[maxn]; memset(tmp,'\n',sizeof(tmp)); int num=0; while(S.top!=S.base) { SElemType e2; GetTop(S,e2); if(e2==ch) { Pop(S); for(int i=num-1; i>=0; i--) Push(S,tmp[i]); return true; } else { tmp[num++]=e2; } Pop(S); } for(int i=num-1; i>=0; i--) Push(S,tmp[i]); return false; } void judge(char ch) { if(ch=='(') printf("(-?\n"); else if(ch=='[') printf("[-?\n"); else if(ch=='{') printf("{-?\n"); else if(ch=='<') printf("/*-?\n"); } int main() { Stack Sta; InitStack(Sta); int flag=1; while(gets(s)) { if(s[0]=='.') break; int len=strlen(s); for(int i=0;i<strlen(s);i++) { if(s[i]=='('||s[i]=='['||s[i]=='{') Push(Sta,s[i]); else if(s[i]=='/'&&s[i+1]=='*'&&i+1<len) { ++i; Push(Sta,'<'); } else if(s[i]==')') { if(Sta.top!=Sta.base) { SElemType e; GetTop(Sta,e); if(e=='(') Pop(Sta); else if(flag) { printf("NO\n"); flag=0; judge(e); } } else if(flag) { flag=0; printf("NO\n"); printf("?-)\n"); } } else if(s[i]==']') { if(Sta.top!=Sta.base) { SElemType e; GetTop(Sta,e); if(e=='[') Pop(Sta); else if(flag) { printf("NO\n"); flag=0; judge(e); } } else if(flag) { flag=0; printf("NO\n"); printf("?-]\n"); } } else if(s[i]=='}') { if(Sta.top!=Sta.base) { SElemType e; GetTop(Sta,e); if(e=='{') Pop(Sta); else if(flag) { printf("NO\n"); flag=0; judge(e); } } else if(flag) { flag=0; printf("NO\n"); printf("?-}\n"); } } else if(s[i]=='*'&&s[i+1]=='/'&&i+1<len) { ++i; if(Sta.top!=Sta.base) { SElemType e; GetTop(Sta,e); if(e=='<') Pop(Sta); else if(flag) { printf("NO\n"); flag=0; judge(e); } } else if(flag) { flag=0; printf("NO\n"); printf("?-*/\n"); } } } } if(flag) { if(Sta.base==Sta.top) printf("YES\n"); else { SElemType e; GetTop(Sta,e); printf("NO\n"); judge(e); } } }符号配对
简单的递归策略非常好理解:
只用一个题举例子:
分析:参数 n,x 两个, 条件 三个;
double P( int n, double x ) { if(n==0) return 1; // 条件一 else if(n==1) return x;//条件二 else if(n>1) { return ((2*n-1)*P(n-1,x)-(n-1)*P(n-2,x))/n;//条件三 } }
这就是根据条件和参数还原一下;
Made by Kindear相关文章推荐
- 【学习点滴-数据结构-栈&队列】 栈的应用--递归的实现-汉诺塔
- 数据结构-栈&队列&Deque实现比较
- 【学习点滴-数据结构-栈&队列】设计一个min函数的栈
- 【图解数据结构】 栈&队列
- 【学习点滴-数据结构-栈&队列】 链式队列的实现及应用
- 【学习点滴-数据结构-栈&队列】 用两个队列模拟一个栈
- 【学习点滴-数据结构-栈&队列】 用两个栈模拟一个队列
- 数据结构-栈&队列&单向链表
- 【学习点滴-数据结构-栈&队列】 颠倒一个栈。
- 【学习点滴-数据结构-栈&队列】 顺序栈的建立,入栈,出栈,判空
- 【学习点滴-数据结构-栈&队列】 栈的应用之一:数值转换
- 【学习点滴-数据结构-栈&队列】 栈的应用之二:括号匹配的检测
- 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks
- 【郝斌数据结构自学笔记】35-46_队列的定义、分类_链式队列伪算法的讲解_循环队列伪算法的讲解
- 【JAVA数据结构】带dummy节点的双向队列
- 数据结构_队列_顺序存储结构队列/链式存储结构队列
- 数据结构-队列-顺序存储完整可执行代码
- 15 API-集合(Collection(功能,迭代器),List(List特有迭代器,并发异常),常见数据结构图示(栈,队列,数组,链表))&对象数组
- 《数据结构》第三章 栈和队列 实验
- [数据结构]第三章-栈和队列(读书笔记3)