赞
踩
第四关
- #include<iostream>
- #include<fstream>
- using namespace std;
-
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- #define MAXSIZE 5 //顺序栈存储空间的初始分配量
- typedef int Status;
- typedef char SElemType;
-
-
- typedef struct
- {int top[2],bot[2]; //栈顶和栈底指针
- SElemType *V; //栈数组
- int m; //栈最大可容纳元素个数
- }DblStack;
-
- //栈初始化
- Status InitDblStack(DblStack &S,int m)
- {
- // ###### Begin ######
- S.m = MAXSIZE;
- S.V = new SElemType[MAXSIZE];
- if(!S.V) return ERROR;
-
- S.bot[0] = 0,S.bot[1] = m - 1;
- S.top[0] = -1,S.top[1] = m;
-
- return OK;
- //###### End ######
- }
-
- Status DblPush(DblStack &S,int i,SElemType x)
- {
- // ###### Begin ######
- if(S.top[0] + 1 == S.top[1]) return ERROR;
-
- if(i == 0) S.V[S.top[0] ++ ] = x;
- else S.V[S.top[1] -- ] = x;
-
- return OK;
-
- //###### End ######
- }
-
- Status DblPop(DblStack &S,int i,SElemType &x)
- {
- // ###### Begin ######
- if(i == 0)
- {
- if(S.top[0] == -1) return ERROR;
- x = S.V[S.top[0] -- ];
- }
- else
- {
- if(S.top[1] == S.m) return ERROR;
- x = S.V[S.top[1] ++ ];
- }
-
- return OK;
- //###### End ######
- }
- int IsEmpty(DblStack S,int i)
- {
- // ###### Begin ######
- if(i == 0 && S.top[0] == -1 || i == 1 && S.top[1] == S.m) return true;
-
- return false;
-
- //###### End ######
- }
- int IsFull(DblStack S)
- {
- // ###### Begin ######
- if(S.top[0] + 1 == S.top[1]) return true;
-
- return false;
-
- //###### End ######
- }
-
- int main() {
- DblStack s;
- int choose, flag = 0;
- int i,m;
- SElemType j, e, t;
-
- choose = -1;
- while (choose != 0) {
-
- cin >> choose;
- switch (choose) {
- case 1://栈的初始化
- {
-
- cin >> m;
- if(InitDblStack(s,m)){
- flag=1;
- cout << "初始化栈成功!" << endl;}
- else
- cout << "初始化栈失败!" << endl;
- break;
- }
- case 2: {//入栈
- if(flag){
-
- cin >> i >> e;
- if(DblPush(s,i,e))
- cout << e << "入栈成功" << endl;
- else
- cout << "栈已满,入栈失败" << endl;
- }
- else
- cout << "栈未建立,请重新选择\n\n";
- break;
- }
- case 3:{//出栈
- if(flag != -1 && flag != 0){
-
-
- cin >> i;
- if(DblPop(s,i,e))
- cout << e << "出栈成功!" << endl;
- else
- cout <<"栈中无元素!" << endl;
- }
- else
- cout << "栈中无元素\n" << endl;
- break;
- }
- case 4:{//判空
-
- cin >> i;
- if(IsEmpty(s,i))
- cout << i << "号栈为空栈"<< endl;
- else
- cout << i << "号栈不为空栈" << endl;
- break;
- }
- case 5:{//判满
- if(IsFull(s))
- cout << "栈满" << endl;
- else
- cout << "栈不满" << endl;
- break;
- }
- default:exit(0);
- }
- }
- }
-
第五关
- //顺序栈定义
- #include<iostream>
- #include<cstring>
- using namespace std;
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- #define MAXSIZE 100 //顺序栈存储空间的初始分配量
- typedef int Status;
- typedef char SElemType;
-
- typedef struct {
- SElemType *base;//栈底指针
- SElemType *top;//栈顶指针
- int stacksize;//栈可用的最大容量
- } SqStack;
-
- //算法3.1 顺序栈的初始化
- Status InitStack(SqStack &S) {
- //构造一个空栈S
- S.base = new SElemType[MAXSIZE];//为顺序栈动态分配一个最大容量为MAXSIZE的数组空间
- if (!S.base)
- exit(OVERFLOW); //存储分配失败
- S.top = S.base; //top初始为base,空栈
- S.stacksize = MAXSIZE; //stacksize置为栈的最大容量MAXSIZE
- return OK;
- }
-
- //算法3.2 顺序栈的入栈
- Status Push(SqStack &S, SElemType e) {
- // 插入元素e为新的栈顶元素
- if (S.top - S.base == S.stacksize)
- return ERROR; //栈满
- *(S.top++) = e; //元素e压入栈顶,栈顶指针加1
- return OK;
- }
-
- //算法3.3 顺序栈的出栈
- Status Pop(SqStack &S, SElemType &e) {
- //删除S的栈顶元素,用e返回其值
- if (S.base == S.top)
- return ERROR;//栈空
- e = *(--S.top); //栈顶指针减1,将栈顶元素赋给e
- return OK;
- }
-
- int EmptyStack(SqStack S){
- return S.base==S.top;
- }
-
- int IsPalindrome(char *t)
- {
- //###### Begin ######
- int len = strlen(t);
-
- int i = 0,j = len - 1;
- while(i < j)
- {
- if(t[i] != t[j]) return 0;
- i ++ ,j -- ;
- }
-
- return 1;
- // ###### End ######
- }
-
- int main(){
-
- char str[MAXSIZE];
- cin>>str;
- if(IsPalindrome(str))
- cout<<"是回文"<<endl;
- else
- cout<<"不是回文"<<endl;
- }
第六关
- #include<iostream>
-
- using namespace std;
-
- //顺序栈定义
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- #define MAXSIZE 100//顺序栈存储空间的初始分配量
- typedef int Status;
- void InoutS(int s[]){
- //###### Begin ######
- int top = 0;
-
- int x;
- while(cin >> x)
- {
- if(x != -1)
- {
- if(top == MAXSIZE - 1) cout << "栈满" << endl;
- else s[top ++ ] = x;
-
- }
- else
- {
- if(top == 0) cout << "栈空" << endl;
- else cout << "出栈元素是" << s[ -- top] << endl;
- }
- }
-
-
- // ###### End ######
- }
- int main(){
- int s[MAXSIZE];
- InoutS(s);
- }
第七关
- #include<iostream>
-
- #include<stdlib.h>
- using namespace std;
-
- //顺序栈定义
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- #define MAXSIZE 100//顺序栈存储空间的初始分配量
- typedef int Status;
- typedef double SElemType;
-
- typedef struct {
- SElemType *base;//栈底指针
- SElemType *top;//栈顶指针
- int stacksize;//栈可用的最大容量
- } SqStack;
-
- //算法3.1 顺序栈的初始化
- Status InitStack(SqStack &S) {
- //构造一个空栈S
- S.base = new SElemType[MAXSIZE];//为顺序栈动态分配一个最大容量为MAXSIZE的数组空间
- if (!S.base)
- exit(OVERFLOW); //存储分配失败
- S.top = S.base; //top初始为base,空栈
- S.stacksize = MAXSIZE; //stacksize置为栈的最大容量MAXSIZE
- return OK;
- }
- //算法3.2 顺序栈的入栈
- Status Push(SqStack &S, SElemType e) {
- // 插入元素e为新的栈顶元素
- if (S.top - S.base == S.stacksize)
- return ERROR; //栈满
- *(S.top++) = e; //元素e压入栈顶,栈顶指针加1
- return OK;
- }
- //算法3.3 顺序栈的出栈
- Status Pop(SqStack &S, SElemType &e) {
- //删除S的栈顶元素,用e返回其值
- if (S.base == S.top)
- return ERROR;//栈空
- e = *(--S.top); //栈顶指针减1,将栈顶元素赋给e
- return OK;
- }
- //算法3.4 取顺序栈的栈顶元素
- double GetTop(SqStack S) {//返回S的栈顶元素,不修改栈顶指针
- if (S.top != S.base) //栈非空
- return *(S.top - 1); //返回栈顶元素的值,栈顶指针不变
- }
-
- int get(char op,int a,int b)
- {
- if(op == '+') return a + b;
- else if(op == '-') return a - b;
- else if(op == '*') return a * b;
- else return a / b;
- }
-
- double Postfix(){
-
- //###### Begin ######
- int *s = new int [MAXSIZE];
-
- char op;
-
- int t = 0,top = 0;
- while(op = getchar(),op != '$')
- {
- if(op == ' ')
- {
- if(t) s[top ++ ] = t;
- t = 0;
- continue;
- }
-
- if(op == '+' || op == '-' || op == '*' || op == '/')
- {
- if(t)
- {
- s[top ++ ] = t;
- t = 0;
- }
-
- int y = s[-- top],x = s[-- top];
- s[top ++ ] = get(op,x,y);
- }
- else t = t * 10 + (op - '0');
- }
-
- int k = top - 1;
- return s[k];
- // ###### End ######
- }
- int main()
- {
- double x;
- x=Postfix();
- cout << x;
- }
第八关
- #include<iostream>
- using namespace std;
- #define MAXSIZE 100
- bool Judge(char A[])
- {
- //###### Begin ######
- int k = 0;
-
- bool flag = true;
- for(int i = 0;A[i] != '\0';i ++ )
- {
- if(A[i] == 'I') k ++ ;
- else
- {
- if(!k)
- {
- flag = false;
- break;
- }
- k -- ;
- }
- }
-
- if(flag) cout << "序列合法" << endl;
- else cout << "序列非法" << endl;
- // ###### End ######
- }
- main()
- {
- char b[MAXSIZE];
- int i;
- cin >> b;
- i=Judge(b);
-
- }
第九关
- #include<iostream>
- using namespace std;
-
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- typedef char QElemType;
- typedef int Status;
-
-
- typedef struct QNode{
- QElemType data;
- struct QNode *next;
- } QNode, *QueuePtr;
-
- typedef struct {
- QueuePtr rear; //只设队尾指针
- } LinkQueue;
-
- void InitQueue(LinkQueue &Q) {//构造一个空队列Q
-
- //###### Begin ######
- Q.rear = new QNode;
- Q.rear -> next = Q.rear;
-
- // ###### End ######
-
- }
-
- int EmptyQueue(LinkQueue Q)
- {
- //###### Begin ######
- if(Q.rear -> next == Q.rear) return OK;
-
- return ERROR;
- // ###### End ######
- }
-
- Status EnQueue(LinkQueue &Q,QElemType e)
- {
- //###### Begin ######
- QueuePtr t = new QNode;
- t -> data = e;
-
- t -> next = Q.rear -> next;
- Q.rear -> next = t;
- Q.rear = t;
-
- return OK;
- // ###### End ######
- }
-
- Status DeQueue(LinkQueue &Q,QElemType &e)
- {
- //###### Begin ######
- if(EmptyQueue(Q)) return ERROR;
-
- QueuePtr p = Q.rear -> next;
- QueuePtr t = p -> next;
- e = t -> data;
-
- p -> next = t -> next;
- if(p -> next == p) Q.rear = p;
- delete t;
-
- return OK;
- // ###### End ######
- }
- main()
- {
- int choose, flag = 0;
- LinkQueue q;
- QElemType j;
-
- choose = -1;
- while (choose != 0) {
-
- cin >> choose;
- switch (choose) {
- case 1:InitQueue(q);flag=1;cout<<"初始化成功\n";break;
- case 2:
- if (flag) {
- flag = 1;
-
- cin >> j;
- EnQueue(q, j);
- cout << j<<"已入队\n";
- }
- else
- cout << "队列未建立,请重新选择\n\n";
- break;
- case 3:
- if (flag){
- flag=1;
- if(DeQueue(q, j))
- cout << j<<"已出队\n";
- else
- cout <<"队列已空,无可出队元素\n";
- }
- else
- cout << "队列未建立,请重新选择\n\n";
- break;
- case 4:
- if(EmptyQueue(q))
- cout <<"此对为空队\n";
- else
- cout<<"此对不空\n";
- break;
- }
- }
- }
第十关
- #include<iostream>
- using namespace std;
-
- #define MAXSIZE 5
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- typedef char ElemType;
- typedef int Status;
-
- typedef struct{
- ElemType *base;
- int front,rear,tag;
- }SqQueue;
-
- Status InitQueue(SqQueue &Q)
- {
- //###### Begin ######
- Q.base = new ElemType [MAXSIZE];
- Q.front = Q.rear = 0;
- Q.tag = 0;
-
- return OK;
- // ###### End ######
- }
- Status EnQueue(SqQueue &Q, ElemType e) {
- //###### Begin ######
- if(Q.tag == 1) return ERROR;
-
- Q.base[Q.rear] = e;
- Q.rear = (Q.rear + 1) % MAXSIZE;
-
- if(Q.front == Q.rear) Q.tag = 1;
- else Q.tag = 2;
-
- return OK;
- // ###### End ######
- }
- Status DeQueue(SqQueue &Q, ElemType &e)
- {
- //###### Begin ######
- if(Q.tag == 0) return ERROR;
-
- e = Q.base[Q.front];
- Q.front = (Q.front + 1) % MAXSIZE;
- if(Q.front == Q.rear) Q.tag = 0;
-
- return OK;
- // ###### End ######
- }
- main()
- {
- SqQueue q;
- int choose,flag;
- ElemType j;
-
- choose = -1;
- while (choose != 0) {
-
- cin >> choose;
- switch (choose) {
- case 1:
- if (InitQueue(q)) {
- flag = 1;
- cout << "成功对队列进行初始化\n";
- } else
- cout << "初始化队列失败\n";
- break;
- case 2:
- if (flag) {
- flag = 1;
-
- cin >> j;
- if(EnQueue(q, j))
- cout << j<<"已入队\n";
- else
- cout <<"队列已满,不可入队\n";
- }
- else
- cout << "队列未建立,请重新选择\n";
- break;
- case 3:
- if (flag){
- flag=1;
- if(DeQueue(q, j))
- cout << j<<"已出队\n";
- else
- cout <<"队列已空,无可出队元素\n";
- }
- else
- cout << "队列未建立,请重新选择\n";
- break;
- }
- }
- }
第十一关
- #include<iostream>
- using namespace std;
-
-
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- #define MAXSIZE 10
- typedef char QElemType;
- typedef int Status;
-
- typedef struct{
- //###### Begin ######
- QElemType *base;
- int front,rear;
-
- // ###### End ######
- }SqQueue;
- Status InitQueue(SqQueue &Q)
- {
- //###### Begin ######
- Q.base = new QElemType [MAXSIZE];
- Q.front = Q.rear = 0;
-
- return OK;
- // ###### End ######
- }
-
- Status EnQueue(SqQueue &Q,QElemType e)
- {
- //###### Begin ######
- if((Q.front + 1) % MAXSIZE == Q.rear) return ERROR;
-
- Q.base[Q.front] = e;
- Q.front = (Q.front + 1) % MAXSIZE;
-
- return OK;
- // ###### End ######
- }
- Status DeQueue(SqQueue &Q,QElemType &e)
- {
- //###### Begin ######
- if(Q.front == Q.rear) return ERROR;
-
- e = Q.base[Q.rear];
- Q.rear = (Q.rear + 1) % MAXSIZE;
-
- return OK;
- // ###### End ######
- }
- main()
- {
- SqQueue q;
- int choose,flag;
- QElemType j;
-
- choose = -1;
- while (choose != 0) {
-
- cin >> choose;
- switch (choose) {
- case 1:
- if (InitQueue(q)) {
- flag = 1;
- cout << "成功对队列进行初始化\n";
- } else
- cout << "初始化队列失败\n";
- break;
- case 2:
- if (flag) {
- flag = 1;
-
- cin >> j;
- if(EnQueue(q, j))
- cout << j<<"已入队\n";
- else
- cout <<"队列已满,不可入队\n";
- }
- else
- cout << "队列未建立,请重新选择\n";
- break;
- case 3:
- if (flag){
- flag=1;
- if(DeQueue(q, j))
- cout << j<<"已出队\n";
- else
- cout <<"队列已空,无可出队元素\n";
- }
- else
- cout << "队列未建立,请重新选择\n";
- break;
- }
- }
- }
第十二关
- #include<iostream>
- #include<stdlib.h>
- #define M 100
- #define N 100
- using namespace std;
- int Ack(int m,int n)//递归算法
- {
- //###### Begin ######
- if(!m) return n + 1;
- else if(!n) return Ack(m - 1,1);
- else return Ack(m - 1,Ack(m,n - 1));
-
-
- // ###### End ######
- }
-
- main(){
- int m,n;
- int x,y;
- cin>>m>>n;
- x=Ack(m,n);
- cout << x <<endl;
-
- }
第十三关
- #include<iostream>
- #include<cstring>
- using namespace std;
- typedef struct LNode {
- int data; //结点的数据域
- struct LNode *next; //结点的指针域
- } LNode, *LinkList; //LinkList为指向结构体LNode的指针类型
-
- void InitList(LinkList &f) //创建链表
- {
- f = new LNode;
- f->next = NULL;
- }
- void ListInput(LinkList &f,int n)
- {
- LinkList r,p;
- int i=1;
- r=p=f;
- cin >> p->data;
- while(i<n)
- {
- p = new LNode;
- cin >> p->data;
- p->next = NULL;
- r->next = p;
- r = p;
- i++;
- }
- cout<<"完成输入\n";
- }
-
- int GetMax(LinkList p)
- {
-
- //###### Begin ######
- LinkList t = p;
-
- int maxv = 0;
-
- while(t)
- {
- maxv = max(maxv,t -> data);
- t = t -> next;
- }
-
- return maxv;
- // ###### End ######
- }
- int GetLength(LinkList p)
- {
- //###### Begin ######
- LinkList t = p;
-
- int length = 0;
-
- while(t)
- {
- length ++ ;
- t = t -> next;
- }
-
- return length;
-
-
- // ###### End ######
- }
- double GetAverage(LinkList p,int n)
- {
- //###### Begin ######
- LinkList t = p;
-
- double sum = 0;
-
- while(t)
- {
- sum += t -> data;
- t = t -> next;
- }
-
- return sum / n;
- // ###### End ######
- }
- main()
- {
- LinkList L;
- int choose,tag=0,n;
- int max,len;
- double ave;
- choose=-1;
- while (choose != 0) {
- cin >> choose;
- switch (choose) {
- case 1: InitList(L);tag=1;cout<<"初始化完成\n";break;
- case 2:
- {
- if(tag==1)
- {
- cin>>n;
- ListInput(L,n);
- }
- break;
- }
- case 3:
- {
- if(tag==1)
- {
- max=GetMax(L);
- cout<<"max="<<max<<endl;
- }
- break;
- }
- case 4:
- {
- if(tag==1)
- {
- len=GetLength(L);
- cout<<"len="<<len<<endl;
- }
- break;
- }
- case 5:
- {
- if(tag==1)
- {
- ave=GetAverage(L,len);
- cout<<"ave="<<ave<<endl;
- }
- break;
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。