1. 用广搜求二叉树的深度
#include "malloc.h"
#include<queue>
//求二叉树的深度
using namespace std;
struct tree
{
int l,r;
}a[1000010];
int s[1000010],maxn=1;
queue<int>q;
void bfs()
{
q.push(1);
s[1]=1;
while(!q.empty())
{
int t=q.front();
if(a[t].l!=0)
{
q.push(a[t].l);
s[a[t].l]=s[t]+1;
if(s[t]+1>maxn)
maxn=s[t]+1;
}
if(a[t].r!=0)
{
q.push(a[t].r);
s[a[t].r]=s[t]+1;
if(s[t]+1>maxn)
maxn=s[t]+1;
}
q.pop();
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].l>>a[i].r;
}
bfs();
cout<<maxn;
}
2. 用深搜求二叉树的深度
#include<iostream>
#include "stdio.h"
//求二叉树的深度
using namespace std;
struct tree
{
int l,r;
}a[1000010];
int maxn;
void dfs(int cur, int s)
{
if(s>maxn)
maxn=s;
if(a[cur].l!=0)
dfs(a[cur].l,s+1);
if(a[cur].r!=0)
dfs(a[cur].r,s+1);
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].l>>a[i].r;
}
dfs(1,1);
cout<<maxn;
}
3.二叉树的遍历
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
struct tree//这里不用定义左子树右子树,因为这里是顺序存储
{
int data;
}a[300];
bool f[1010];int n;
void xx(int root)
{
cout<<a[root].data<<" ";
if(root*2<=n) xx(root*2);
if(root*2+1<=n) xx(root*2+1);
}
void zx(int root)
{
if(root*2<=n) xx(root*2);
cout<<a[root].data<<" ";
if(root*2+1<=n) xx(root*2+1);
}
void hx(int root)
{
if(root*2<=n) xx(root*2);
if(root*2+1<=n) xx(root*2+1);
cout<<a[root].data<<" ";
}
int main()
{
int h,p;
cin>>h>>p;
n=pow(2,h)-1;
int s=1,t;
while(s<=n&&cin>>t)
{
if(f[t]==0)
{
f[t]=1;
a[s].data=t;
s++;
}
}
if(p==1) xx(1);
if(p==2) zx(1);
if(p==3) hx(1);
}
4.树例题:
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
struct tree
{
char data;
int l,r;
}a[27];
bool f[27];
void xx(int root)
{
cout<<a[root].data;
if(a[root].l!=0)
xx(a[root].l);
if(a[root].r!=0)
xx(a[root].r);
}
void zx(int root)
{
if(a[root].l!=0)
zx(a[root].l);
cout<<a[root].data;
if(a[root].r!=0)
zx(a[root].r);
}
void hx(int root)
{
if(a[root].l!=0)
hx(a[root].l);
if(a[root].r!=0)
hx(a[root].r);
cout<<a[root].data;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].data>>a[i].l>>a[i].r;
f[a[i].l]=1;f[a[i].r]=1;
}
int root;
for(int i=1;i<=n;i++)
{
if(f[i]==0)
{
root=i;break;
}
}
xx(root);
cout<<endl;
zx(root);
cout<<endl;
hx(root);
}
5.输入数据,建立一个先序二叉树,中序遍历输出
例:输入a#b#cdef##### 输出 a b f e d c
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
struct tree
{
char data;
int l,r;
}a[110];
int s=1;
char t;
void dfs()
{
int cur=s;//先找cur的左孩子
cin>>t;
if(t!='#')
{
s++;
a[cur].l=s;
a[s].data=t;
dfs();
}
cin>>t;//再找cur的右孩子
if(t!='#')
{
s++;
a[cur].r=s;
a[s].data=t;
dfs();
}
}
void zx(int root)
{
if(a[root].l!=0) zx(a[root].l);
cout<<a[root].data<<" ";
if(a[root].r!=0) zx(a[root].r);
}
int main()
{
cin>>t;
a[s].data=t;
dfs();
zx(1);
}
6. 二叉树的遍历运算 oj输入先序和中序,输出后序
例:输入:ADBGCEFH DGBAECHF 输出:GBDEHFCA
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
using namespace std;
void hx(string q, string z)
{
int len_q=q.size(),len_z=z.size();
if(len_q==0||len_z==0) return ;
char root=q[0];
int t=z.find(root);
//左
hx(q.substr(1,t), z.substr(0,t));
//右
hx(q.substr(t+1,len_q-t), z.substr(t+1,len_z-t));
//根
cout<<root;
}
int main()
{
string q,z;
cin>>q>>z;
hx(q,z);
}
7. 同上一题,输入中序和后序,输出前序
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
using namespace std;
void dfs(string z, string h)
{
int len_z=z.size(), len_h=h.size();
if(len_z==0||len_h==0) return ;
char root=h[len_h-1];
int t=z.find(root);
cout<<root;
dfs(z.substr(0,t), h.substr(0,t));
dfs(z.substr(t+1,len_z-1-t), h.substr(t,len_h-1-t));
}
int main()
{
string z,h;
cin>>z>>h;
dfs(z,h);
}
18924.oj 求二叉树的宽度
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n, v[55];
struct tree
{
int p, l, r;
}a[105];
void t(int fu, int zi)
{
a[zi].p=fu;
if(!a[fu].l) a[fu].l=zi;
else a[fu].r=zi;
}
void l(int root, int c)
{
v[c]++;
if(a[root].l) l(a[root].l, c+1);
if(a[root].r) l(a[root].r, c+1);
}
int main()
{
int i,x,y,root,cnt;
cin>>n;
for(i=1;i<n;i++)
{
cin>>x>>y;
t(x,y);
}
for(i=1;i<=n;i++)
if(a[i].p==0)
{
root=i;
break;
}
l(root,1);
for(i=1;i<=50;i++)
if(v[i]>cnt)
cnt=v[i];
cout<<cnt;
}
18923.oj 求二叉树的直径
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n,child[105][3],ans=0;
int dfs(int root)
{
if(!root)
return 0;
int lchild=dfs(child[root][1]),rchild=dfs(child[root][2]);
int len=max(lchild,rchild)+1;
ans=max(ans,lchild+rchild);
return len;
}
int main()
{
int i,j,x,y;
cin>>n;
for(i=1; i<n; i++)
{
cin>>x>>y;
if(!child[x][1])
child[x][1]=y;
else
child[x][2]=y;
}
dfs(1);
cout<<ans;
return 0;
}
#include<iostream>
#include "stdio.h"
#include "malloc.h"
using namespace std;
typedef int ElemType;
typedef struct BiNode
{
ElemType data;
struct BiNode *lchild, *rchild;
}BiNode, *BiTree;
void insertData(BiTree &T, ElemType data)
{
BiTree P = new BiNode;
P->data=data;
P->lchild=NULL;
P->rchild=NULL;
if(T==NULL) T=P;
else
{
if(T->lchild==NULL) T->lchild=P;
else T->rchild=P;
}
}
void preOrderSearch (BiTree T, ElemType x, BiTree &result)
{
if(T)
{
if(T->data==x) result=T;
preOrderSearch(T->lchild, x, result);
preOrderSearch(T->rchild, x, result);
}
}
int maxmeter = 0;
int maxdepth(BiTree &T)
{
if(T==NULL) return 0;
int leftdepth=maxdepth(T->lchild);
int rightdepth=maxdepth(T->rchild);
int meter=leftdepth+rightdepth;
maxmeter=meter>maxmeter?meter:maxmeter;
return leftdepth>rightdepth?leftdepth+1:rightdepth+1;
}
int main()
{
BiTree P=NULL;
int n;cin>>n;n--;
insertData(P,1);
while(n--)
{
ElemType x,y;
cin>>x>>y;
BiTree res=NULL;
preOrderSearch(P,x,res);
insertData(res,y);
}
maxdepth(P);
cout<<maxmeter;
}
求二叉树的最长路径: 输入:1234##5 输出:4 按完全二叉树形式存储
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,ans=0;
string s;
int dfs(int root)
{
if(s[root-1]=='#'||root>s.size()) return 0;
int l=dfs(2*root), r=dfs(2*root+1);
ans=max(ans,l+r);
return max(l,r)+1;
}
int main()
{
cin>>n>>s;
dfs(1);
cout<<ans;
}
栈的应用 ——进制转换
#include <iostream>
#include <stack>
using namespace std;
stack <int> S;
int main()
{
int n;
cin>>n;
while(n)
{
S.push(n%8);
n/=8;
}
while(!S.empty())
{
cout<<S.top();
S.pop();
}
return 0;
}
8591.计算next值
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#define MAXSTRLEN 255 // 用户可在255以内定义最大串长
typedef unsigned char SString[MAXSTRLEN+1]; // 0号单元存放串的长度
void get_next(SString T,int next[]){
// 算法4.7
// 求模式串T的next函数值并存入数组next
// 请补全代码
next[1]=0;
int i=1,j=0;
while(i<=T[0])
{
if((j==0)||T[i]==T[j])
next[++i]=++j;
else j=next[j];
}
}
int main(){
int next[MAXSTRLEN];
SString S;
int n,i,j;
char ch;
scanf("%d",&n); // 指定要验证NEXT值的字符串个数
ch=getchar();
for(i=1;i<=n;i++)
{
ch=getchar();
for(j=1;j<=MAXSTRLEN&&(ch!='\n');j++) // 录入字符串
{
S[j]=ch;
ch=getchar();
}
S[0]=j-1; // S[0]用于存储字符串中字符个数
get_next(S,next);
printf("NEXT J is:");
for(j=1;j<=S[0];j++)
printf("%d",next[j]);
printf("\n");
}
}
8576.顺序线性表的基本操作
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L)
{
// 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
// 请补全代码
}
int Load_Sq(SqList &L)
{
// 输出顺序表中的所有元素
int i;
if(_________________________) printf("The List is empty!"); // 请填空
else
{
printf("The List is: ");
for(_________________________) printf("%d ",_________________________); // 请填空
}
printf("\n");
return OK;
}
int ListInsert_Sq(SqList &L,int i,int e)
{
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
// 请补全代码
}
int ListDelete_Sq(SqList &L,int i, int &e)
{
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码
}
int main()
{
SqList T;
int a, i;
ElemType e, x;
if(_________________________) // 判断顺序表是否创建成功
{
printf("A Sequence List Has Created.\n");
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(_________________________) printf("Insert Error!\n"); // 执行插入函数,根据返回值判断i值是否合法
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d",&i);
if(_________________________) printf("Delete Error!\n"); // 执行删除函数,根据返回值判断i值是否合法
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: Load_Sq(T);
break;
case 0: return 1;
}
}
}
输入格式
测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束
输入样例
1
1 2
1
1 3
2
1
3
0
输出样例
A Sequence List Has Created.
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 2 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Deleted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The List is: 2
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L.listsize=LIST_INIT_SIZE;
L.length=0;
return OK;
}
int Load_Sq(SqList &L)
{
// 输出顺序表中的所有元素
int i;
if(L.length==0) printf("The List is empty!"); // 请填空
else
{
printf("The List is: ");
for(i=0;i<L.length;i++) printf("%d ",L.elem[i]); // 请填空
}
printf("\n");
return OK;
}
int ListInsert_Sq(SqList &L,int i,int e)
{
if(i<1||i>L.length+1) return 0;
for(int j=L.length-1;j>=i-1;j--) L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
L.length++;
return OK;
}
int ListDelete_Sq(SqList &L,int i, int &e)
{
if(i<1||i>L.length) return 0;
e=L.elem[i-1];
for(int j=i-1;j<L.length;j++)L.elem[j]=L.elem[j+1];
L.length--;
return OK;
}
int main()
{
SqList T;
int a, i;
ElemType e, x;
if(InitList_Sq(T)) // 判断顺序表是否创建成功
{
printf("A Sequence List Has Created.\n");
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(!ListInsert_Sq(T,i,x)) printf("Insert Error!\n"); // 执行插入函数,根据返回值判断i值是否合法
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d",&i);
if(!ListDelete_Sq(T,i,e)) printf("Delete Error!\n"); // 执行删除函数,根据返回值判断i值是否合法
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: Load_Sq(T);
break;
case 0: return 1;
}
}
}
8577.合并顺序表
若线性表中数据元素相互之间可以比较,且数据元素在表中按值递增或递减,则称该表为有序表。
编写算法,将两个非递减有序顺序表A和B合并成一个新的非递减有序顺序表C。
输入格式
第一行:顺序表A的元素个数
第二行:顺序表A的各元素(非递减),用空格分开
第三行:顺序表B的元素个数
第四行:顺序表B的各元素(非递减),用空格分开
输出格式
第一行:顺序表A的元素列表
第二行:顺序表B的元素列表
第三行:合并后顺序表C的元素列表
输入样例
5
1 3 5 7 9
5
2 4 6 8 10
输出样例
List A:1 3 5 7 9
List B:2 4 6 8 10
List C:1 2 3 4 5 6 7 8 9 10
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
using namespace std;
typedef int Status;
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
//构造一个空的线性表
Status InitList_Sq(SqList &L)
{
L.elem=new ElemType[LIST_INIT_SIZE];//分配空间
if(!L.elem) return ERROR;
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
//向线性表中输入值
Status filllist_Sq(SqList &L)
{
int n;
cin>>n;
int i, j;
for(i=0;i<n;i++)
{
cin>>j;
L.elem[i]=j;
L.length++;
}
return OK;
}
//合并线性表
void MergeList_Sq(SqList LA, SqList LB, SqList &LC)
{
int i=0, j=0, k=0;
while((i<LA.length)&&(j<LB.length))
{
if(LA.elem[i]<=LB.elem[j])
{
LC.elem[k++]=LA.elem[i++];
LC.length++;
}
else
{
LC.elem[k++]=LB.elem[j++];
LC.length++;
}
}
while (i<LA.length)
{
LC.elem[k++]=LA.elem[i++];
LC.length++;
}
while (j<LB.length)
{
LC.elem[k++]=LB.elem[j++];
LC.length++;
}
}
//打印线性表
void printlist_Sq(SqList L)
{
int i;
for(i=0;i<L.length;i++)
{
printf("%d ",L.elem[i]);
}
printf("\n");
}
int main()
{
SqList A,B,C;
InitList_Sq(A);
InitList_Sq(B);
InitList_Sq(C);
filllist_Sq(A);
filllist_Sq(B);
MergeList_Sq(A,B,C);
printf("List A:");
printlist_Sq(A);
printf("List B:");
printlist_Sq(B);
printf("List C:");
printlist_Sq(C);
return 0;
}
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int a,b,i,j,temp;
cin>>a;
int A[a];
for(i=0;i<a;i++) cin>>A[i];
cin>>b;
int B[b];
for(i=0;i<b;i++) cin>>B[i];
int c=a+b, C[c];
for(i=0;i<a;i++) C[i]=A[i];
for(i=a,j=0;i<c,j<b;i++,j++) C[i]=B[j];
for(i=0;i<c;i++)
{
for(j=0;j<c-1-i;j++)
{
if(C[j]>C[j+1])
{
temp=C[j];
C[j]=C[j+1];
C[j+1]=temp;
}
}
}
cout<<"List A:";
for(i=0;i<a;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
cout<<"List B:";
for(i=0;i<b;i++)
{
cout<<B[i]<<" ";
}
cout<<endl;
cout<<"List C:";
for(i=0;i<c;i++)
{
cout<<C[i]<<" ";
}
}
8578. 逆置顺序表
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int n,i;
cin>>n;
int a[n];
for(i=0;i<n;i++)
cin>>a[i];
cout<<"The List is:";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"The turned List is:";
for(i=n-1;i>=0;i--)
cout<<a[i]<<" ";
}
8578.链式线性表的基本操作
Description 编写算法,创建一个含有n个元素的带头结点的单链表L并实现插入、删除、遍历操作。本题目提供部分代码,请补全内容。
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define ElemType int
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
int CreateLink_L(LinkList &L,int n){
// 创建含有n个元素的单链表
LinkList p,q;
int i;
ElemType e;
L = new LNode;
L->next = NULL; // 先建立一个带头结点的单链表
q = L;
for (i=0; i<n; i++) {
scanf("%d", &e);
p = new LNode; // 生成新结点
// 请补全代码
}
return OK;
}
int LoadLink_L(LinkList &L){
// 单链表遍历
LinkList p = L->next;
if(___________________________)printf("The List is empty!"); // 请填空
else
{
printf("The LinkList is:");
while(___________________________) // 请填空
{
printf("%d ",p->data);
___________________________ // 请填空
}
}
printf("\n");
return OK;
}
int LinkInsert_L(LinkList &L,int i,ElemType e){
// 算法2.9
// 在带头结点的单链线性表L中第i个位置之前插入元素e
// 请补全代码
}
int LinkDelete_L(LinkList &L,int i, ElemType &e){
// 算法2.10
// 在带头结点的单链线性表L中,删除第i个元素,并用e返回其值
// 请补全代码
}
int main()
{
LinkList T;
int a,n,i;
ElemType x, e;
printf("Please input the init size of the linklist:\n");
scanf("%d",&n);
printf("Please input the %d element of the linklist:\n", n);
if(___________________________) // 判断链表是否创建成功,请填空
{
printf("A Link List Has Created.\n");
LoadLink_L(T);
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(___________________________) printf("Insert Error!\n"); // 判断i值是否合法,请填空
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d",&i);
if(___________________________) printf("Delete Error!\n"); // 判断i值是否合法,请填空
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: LoadLink_L(T);
break;
case 0: return 1;
}
}
}
输入格式
测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束
输入样例
3
3 6 9
3
1
4 12
2
1
3
0
输出样例
Please input the init size of the linklist:
Please input the 3 element of the linklist:
A Link List Has Created.
The LinkList is:3 6 9
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The LinkList is:3 6 9
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 12 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Deleted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The LinkList is:6 9 12
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define ElemType int
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
int CreateLink_L(LinkList &L,int n){
// 创建含有n个元素的单链表
LinkList p,q;
int i;
ElemType e;
L = new LNode;
L->next = NULL; // 先建立一个带头结点的单链表
q = L;
for (i=0; i<n; i++) {
scanf("%d", &e);
p = new LNode; // 生成新结点
// 请补全代码
p->data=e;//给链表元素赋值
p->next=NULL;//留条后路
q->next=p;
q=p;
}
return OK;
}
//输出链表
int LoadLink_L(LinkList &L){
// 单链表遍历
LinkList p = L->next;
if(!p)printf("The List is empty!"); // 请填空
else
{
printf("The LinkList is:");
while(p) // 请填空
{
printf("%d ",p->data);
p=p->next; // 请填空
}
}
printf("\n");
return OK;
}
//插入元素
int LinkInsert_L(LinkList &L,int i,ElemType e)
{
int j=0;
LNode *p, *q;
p=L;
while(p&&(j<i-1))
{
p=p->next;
j++;
}
if(!p||j>i-1) return ERROR;
q=new LNode;
q->data=e;
q->next=p->next;//向后移动
p->next=q;
return OK;
// 算法2.9
// 在带头结点的单链线性表L中第i个位置之前插入元素e
// 请补全代码
}
//删除元素
int LinkDelete_L(LinkList &L,int i, ElemType &e)
{
int j=0;
LNode *p, *q;
p=L;
while((p->next)&&(j<i-1))
{
p=p->next;
j++;
}
if(!(p->next)||(j>i-1)) return ERROR;
q=p->next;//将找到的元素的地址存下来
p->next=q->next;//删除该元素
e=q->data;//对e赋值
delete q;
return OK;
// 算法2.10
// 在带头结点的单链线性表L中,删除第i个元素,并用e返回其值
// 请补全代码
}
int main()
{
LinkList T;
int a,n,i;
ElemType x, e;
printf("Please input the init size of the linklist:\n");
scanf("%d",&n);
printf("Please input the %d element of the linklist:\n", n);
if(CreateLink_L(T,n)) // 判断链表是否创建成功,请填空
{
printf("A Link List Has Created.\n");
LoadLink_L(T);
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(!LinkInsert_L(T,i,x)) printf("Insert Error!\n"); // 判断i值是否合法,请填空
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d",&i);
if(!LinkDelete_L(T,i,e)) printf("Delete Error!\n"); // 判断i值是否合法,请填空
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: LoadLink_L(T);
break;
case 0: return 1;
}
}
}
8580.合并链表
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int a,b,i,j,temp;
cin>>a;
int A[a];
for(i=0;i<a;i++) cin>>A[i];
cin>>b;
int B[b];
for(i=0;i<b;i++) cin>>B[i];
int c=a+b, C[c];
for(i=0;i<a;i++) C[i]=A[i];
for(i=a,j=0;i<c,j<b;i++,j++) C[i]=B[j];
for(i=0;i<c;i++)
{
for(j=0;j<c-1-i;j++)
{
if(C[j]>C[j+1])
{
temp=C[j];
C[j]=C[j+1];
C[j+1]=temp;
}
}
}
cout<<"List A:";
for(i=0;i<a;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
cout<<"List B:";
for(i=0;i<b;i++)
{
cout<<B[i]<<" ";
}
cout<<endl;
cout<<"List C:";
for(i=0;i<c;i++)
{
cout<<C[i]<<" ";
}
}
19080. 反转链表
一道经典的题目
给定一个单链表的头结点L,长度为n,反转该链表后,返回新链表的表头。
要求:空间复杂度 O(1) ,时间复杂度 O(n)。
如当输入链表{1,2,3}时,
经反转后,原链表变为{3,2,1},所以对应的输出为{3,2,1}。
#include <iostream>//C++
using namespace std;
struct LNode
{
int data;
LNode * next;
};
void createList(LNode * &L,int n)
{
/**< 尾插法创建单链表 */
LNode *r, *p;
r=L=new LNode;/**< 创建头结点 */
L->next=NULL;
for(int i=1; i<=n; i++)
{
p=new LNode;
cin>>p->data;
p->next=NULL;
r->next=p;
r=p;
}
}
void trv(LNode * L)
{
/**< 一个简单的链表遍历函数,供编程过程中测试使用 */
L=L->next;
while(L)
{
cout<<L->data<<' ';
L=L->next;
}
}
void reverseList(LNode * &L)
{
LNode *pre = NULL;/**< 用三个指针分别表示前驱,当前,后继 */
LNode *cur = L->next;/**< 当前是第一个节点a1 */
LNode *nex = NULL; /**<思考如何用这三个指针实现翻转,另外,三个指针也要同步后移 */
while (cur)
{
_______________________
}
L->next=pre;
}
int main()
{
int n;
LNode *L;
cin>>n;
createList(L,n);
reverseList(L);
trv(L);
return 0;
}
输入格式
第一行一个整数n,代表链表长度。
第二行n个整数。
输出格式
输出逆置后的单链表。
输入样例
5
1 2 3 4 5
输出样例
5 4 3 2 1
#include <iostream>//C++
using namespace std;
struct LNode
{
int data;
LNode * next;
};
void createList(LNode * &L,int n)
{
/**< 尾插法创建单链表 */
LNode *r, *p;
r=L=new LNode;/**< 创建头结点 */
L->next=NULL;
for(int i=1; i<=n; i++)
{
p=new LNode;
cin>>p->data;
p->next=NULL;
r->next=p;
r=p;
}
}
void trv(LNode * L)
{
/**< 一个简单的链表遍历函数,供编程过程中测试使用 */
L=L->next;
while(L)
{
cout<<L->data<<' ';
L=L->next;
}
}
void reverseList(LNode * &L)
{
LNode *pre = NULL;/**< 用三个指针分别表示前驱,当前,后继 */
LNode *cur = L->next;/**< 当前是第一个节点a1 */
LNode *nex = NULL; /**<思考如何用这三个指针实现翻转,另外,三个指针也要同步后移 */
while (cur)
{
nex = cur->next;
cur->next = pre;
pre = cur;
cur = nex;
}
L->next=pre;
}
int main()
{
int n;
LNode *L;
cin>>n;
createList(L,n);
reverseList(L);
trv(L);
return 0;
}
8583.顺序栈的基本操作
Description 创建一个空的顺序栈,并实现栈的入栈、出栈、返回栈的长度、返回栈顶元素、栈的遍历等基本算法。请将下面的程序补充完整。
#include<malloc.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量
typedef int SElemType; // 定义栈元素类型
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{
// 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE
// 请补全代码
}
Status Push(SqStack &S,SElemType e)
{
// 在栈S中插入元素e为新的栈顶元素
// 请补全代码
}
Status Pop(SqStack &S,SElemType &e)
{
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
// 请补全代码
}
Status GetTop(SqStack S,SElemType &e)
{
// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
// 请补全代码
}
int StackLength(SqStack S)
{
// 返回栈S的元素个数
// 请补全代码
}
Status StackTraverse(SqStack S)
{
// 从栈顶到栈底依次输出栈中的每个元素
SElemType *p = ______________________ //请填空
if(______________________)printf("The Stack is Empty!"); //请填空
else
{
printf("The Stack is: ");
while(______________________) //请填空
{
______________________ //请填空
printf("%d ", *p);
}
}
printf("\n");
return OK;
}
int main()
{
int a;
SqStack S;
SElemType x, e;
if(______________________) // 判断顺序表是否创建成功,请填空
{
printf("A Stack Has Created.\n");
}
while(1)
{
printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d", &x);
if(______________________) printf("Push Error!\n"); // 判断Push是否合法,请填空
else printf("The Element %d is Successfully Pushed!\n", x);
break;
case 2: if(______________________) printf("Pop Error!\n"); // 判断Pop是否合法,请填空
else printf("The Element %d is Successfully Poped!\n", e);
break;
case 3: if(______________________)printf("Get Top Error!\n"); // 判断Get Top是否合法,请填空
else printf("The Top Element is %d!\n", e);
break;
case 4: printf("The Length of the Stack is %d!\n",______________________); //请填空
break;
case 5: ______________________ //请填空
break;
case 0: return 1;
}
}
}
输入格式
测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现Push操作,紧跟着输入要Push的元素
2、输入2,表示要实现Pop操作
3、输入3,返回栈顶元素
4、输入4,返回栈的元素个数
5、输入5,表示从栈顶到栈底输出栈的所有元素
6、输入0,表示程序结束
输入样例
1
2
1
4
1
6
5
3
4
2
5
2
2
2
0
输出样例
A Stack Has Created.
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Element 2 is Successfully Pushed!
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Element 4 is Successfully Pushed!
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Element 6 is Successfully Pushed!
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Stack is: 6 4 2
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Top Element is 6!
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Length of the Stack is 3!
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Element 6 is Successfully Poped!
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Stack is: 4 2
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Element 4 is Successfully Poped!
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
The Element 2 is Successfully Poped!
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
Pop Error!
1:Push
2:Pop
3:Get the Top
4:Return the Length of the Stack
5:Load the Stack
0:Exit
Please choose:
#include<malloc.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量
typedef int SElemType; // 定义栈元素类型
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{
// 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE
// 请补全代码
S.base=(SElemType*)malloc(STACK_INIT_SIZE* sizeof(SElemType));
if(!S.base) return ERROR;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,SElemType e)
{
// 在栈S中插入元素e为新的栈顶元素
// 请补全代码
if(S.top-S.base==S.stacksize)
return ERROR;
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
// 请补全代码
if(S.top==S.base)
return ERROR;
e=*--S.top;
return OK;
}
Status GetTop(SqStack S,SElemType &e)
{
// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
// 请补全代码
if(S.top==S.base)
return ERROR;
e=*(S.top-1);
return OK;
}
int StackLength(SqStack S)
{
// 返回栈S的元素个数
// 请补全代码
return S.top-S.base;
}
Status StackTraverse(SqStack S)
{
// 从栈顶到栈底依次输出栈中的每个元素
SElemType *p = (SElemType*)malloc(sizeof(SElemType));
p=S.top; //请填空
if(S.top==S.base)printf("The Stack is Empty!"); //请填空
else
{
printf("The Stack is: ");
p--;
while(p>=S.base) //请填空
{
//请填空
printf("%d ", *p);
p--;
}
}
printf("\n");
return OK;
}
int main()
{
int a;
SqStack S;
SElemType x, e;
if(InitStack(S)) // 判断顺序表是否创建成功,请填空
{
printf("A Stack Has Created.\n");
}
while(1)
{
printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d", &x);
if(!Push(S,x)) printf("Push Error!\n"); // 判断Push是否合法,请填空
else printf("The Element %d is Successfully Pushed!\n", x);
break;
case 2: if(!Pop(S,e)) printf("Pop Error!\n"); // 判断Pop是否合法,请填空
else printf("The Element %d is Successfully Poped!\n", e);
break;
case 3: if(!GetTop(S,e))printf("Get Top Error!\n"); // 判断Get Top是否合法,请填空
else printf("The Top Element is %d!\n", e);
break;
case 4: printf("The Length of the Stack is %d!\n",StackLength(S)); //请填空
break;
case 5: StackTraverse(S); //请填空
break;
case 0: return 1;
}
}
}
8584.循环队列的基本操作
Description 创建一个空的循环队列,并实现入队、出队、返回队列的长度、返回队头元素、队列的遍历等基本算法。请将下面的程序补充完整。
#include<malloc.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int QElemType;
#define MAXQSIZE 100 // 最大队列长度(对于循环队列,最大队列长度要减1)
typedef struct
{
QElemType *base; // 初始化的动态分配存储空间
int front; // 头指针,若队列不空,指向队列头元素
int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
Status InitQueue(SqQueue &Q)
{
// 构造一个空队列Q,该队列预定义大小为MAXQSIZE
// 请补全代码
}
Status EnQueue(SqQueue &Q,QElemType e)
{
// 插入元素e为Q的新的队尾元素
// 请补全代码
}
Status DeQueue(SqQueue &Q, QElemType &e)
{
// 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR
// 请补全代码
}
Status GetHead(SqQueue Q, QElemType &e)
{
// 若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR
// 请补全代码
}
int QueueLength(SqQueue Q)
{
// 返回Q的元素个数
// 请补全代码
}
Status QueueTraverse(SqQueue Q)
{
// 若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.
int i;
i=Q.front;
if(______________________)printf("The Queue is Empty!"); //请填空
else{
printf("The Queue is: ");
while(______________________) //请填空
{
printf("%d ",______________________ ); //请填空
i = ______________________; //请填空
}
}
printf("\n");
return OK;
}
int main()
{
int a;
SqQueue S;
QElemType x, e;
if(______________________) // 判断顺序表是否创建成功,请填空
{
printf("A Queue Has Created.\n");
}
while(1)
{
printf("1:Enter \n2:Delete \n3:Get the Front \n4:Return the Length of the Queue\n5:Load the Queue\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d", &x);
if(______________________) printf("Enter Error!\n"); // 判断入队是否合法,请填空
else printf("The Element %d is Successfully Entered!\n", x);
break;
case 2: if(______________________) printf("Delete Error!\n"); // 判断出队是否合法,请填空
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: if(______________________)printf("Get Head Error!\n"); // 判断Get Head是否合法,请填空
else printf("The Head of the Queue is %d!\n", e);
break;
case 4: printf("The Length of the Queue is %d!\n",______________________); //请填空
break;
case 5: ______________________ //请填空
break;
case 0: return 1;
}
}
}
输入格式
测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现入队操作,紧跟着输入要入队的元素
2、输入2,表示要实现出队操作
3、输入3,返回队头元素
4、输入4,返回队列的元素个数
5、输入5,表示从队头到队尾输出队的所有元素
6、输入0,表示程序结束
输入样例
1
1
1
2
1
3
5
2
3
5
0
输出样例
A Queue Has Created.
1:Enter
2:Delete
3:Get the Front
4:Return the Length of the Queue
5:Load the Queue
0:Exit
Please choose:
The Element 1 is Successfully Entered!
1:Enter
2:Delete
3:Get the Front
4:Return the Length of the Queue
5:Load the Queue
0:Exit
Please choose:
The Element 2 is Successfully Entered!
1:Enter
2:Delete
3:Get the Front
4:Return the Length of the Queue
5:Load the Queue
0:Exit
Please choose:
The Element 3 is Successfully Entered!
1:Enter
2:Delete
3:Get the Front
4:Return the Length of the Queue
5:Load the Queue
0:Exit
Please choose:
The Queue is: 1 2 3
1:Enter
2:Delete
3:Get the Front
4:Return the Length of the Queue
5:Load the Queue
0:Exit
Please choose:
The Element 1 is Successfully Deleted!
1:Enter
2:Delete
3:Get the Front
4:Return the Length of the Queue
5:Load the Queue
0:Exit
Please choose:
The Head of the Queue is 2!
1:Enter
2:Delete
3:Get the Front
4:Return the Length of the Queue
5:Load the Queue
0:Exit
Please choose:
The Queue is: 2 3
1:Enter
2:Delete
3:Get the Front
4:Return the Length of the Queue
5:Load the Queue
0:Exit
Please choose:
#include<malloc.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int QElemType;
#define MAXQSIZE 100 // 最大队列长度(对于循环队列,最大队列长度要减1)
typedef struct
{
QElemType *base; // 初始化的动态分配存储空间
int front; // 头指针,若队列不空,指向队列头元素
int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
//队列的初始化
Status InitQueue(SqQueue &Q)
{
Q.base=new QElemType[MAXQSIZE];//分配储存空间
if(!Q.base) return ERROR;//储存空间分配失败
Q.front=Q.rear=0;//头指针尾指针置为零,队列为空
return OK;
// 构造一个空队列Q,该队列预定义大小为MAXQSIZE
// 请补全代码
}
//元素插入
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;//队满
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;//新元素加入队尾
return OK;
// 插入元素e为Q的新的队尾元素
// 请补全代码
}
Status DeQueue(SqQueue &Q, QElemType &e)
{
if(Q.rear==Q.front) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
// 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR
// 请补全代码
}
//取队头元素
Status GetHead(SqQueue Q, QElemType &e)
{
if(Q.front==Q.rear)
return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
// 若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR
// 请补全代码
}
//求队列长度
int QueueLength(SqQueue Q)
{
return ((Q.rear-Q.front+MAXQSIZE)%MAXQSIZE);
// 返回Q的元素个数
// 请补全代码
}
Status QueueTraverse(SqQueue Q)
{
// 若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.
int i;
i=Q.front;
if(i==Q.rear)printf("The Queue is Empty!"); //请填空
else{
printf("The Queue is: ");
while(i!=Q.rear) //请填空
{
printf("%d ", Q.base[i]); //请填空
i = i+1; //请填空
}
}
printf("\n");
return OK;
}
int main()
{
int a;
SqQueue S;
QElemType x, e;
if(InitQueue(S)) // 判断顺序表是否创建成功,请填空
{
printf("A Queue Has Created.\n");
}
while(1)
{
printf("1:Enter \n2:Delete \n3:Get the Front \n4:Return the Length of the Queue\n5:Load the Queue\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d", &x);
if(!EnQueue(S,x)) printf("Enter Error!\n"); // 判断入队是否合法,请填空
else printf("The Element %d is Successfully Entered!\n", x);
break;
case 2: if(!DeQueue(S,e)) printf("Delete Error!\n"); // 判断出队是否合法,请填空
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: if(!GetHead(S,e))printf("Get Head Error!\n"); // 判断Get Head是否合法,请填空
else printf("The Head of the Queue is %d!\n", e);
break;
case 4: printf("The Length of the Queue is %d!\n",QueueLength(S)); //请填空
break;
case 5: QueueTraverse(S); //请填空
break;
case 0: return 1;
}
}
}
8586.括号匹配检验
Description 利用栈编写满足下列要求的括号匹配检验程序:假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,即([]())或[([][])]等为正确的格式,[(]或([())或(()])均为不正确的格式。输入一个包含上述括号的表达式,检验括号是否配对。本题给出部分check()函数,要求将check()函数补充完整,并完成整个程序。
typedef char SElemType;
#include"malloc.h"
#include"stdio.h"
#include"math.h"
#include"stdlib.h" // exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
#define STACK_INIT_SIZE 10 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{
}
Status StackEmpty(SqStack S)
{
}
Status Push(SqStack &S,SElemType e)
{
}
Status Pop(SqStack &S,SElemType &e)
{
}
void check()
{ // 对于输入的任意一个字符串,检验括号是否配对
SqStack s;
SElemType ch[80],*p,e;
if(InitStack(s)) // 初始化栈成功
{
//printf("请输入表达式\n");
__________________________________;
p=ch;
while(*p) // 没到串尾
switch(*p)
{
case '(':
case '[':_______________________;
break; // 左括号入栈,且p++
case ')':
case ']':if(!StackEmpty(s)) // 栈不空
{
_________________________; // 弹出栈顶元素
if(*p==')'&&e!='('||___________________&&___________________)
// 弹出的栈顶元素与*p不配对
{
printf("isn't matched pairs\n");
exit(ERROR);
}
else
{
__________________________;
break; // 跳出switch语句
}
}
else // 栈空
{
printf("lack of left parenthesis\n");
exit(ERROR);
}
default: ______________________; // 其它字符不处理,指针向后移
}
if(StackEmpty(s)) // 字符串结束时栈空
printf("matching\n");
else
printf("lack of right parenthesis\n");
}
}
int main()
{
check();
}
输入格式
第一行:输入一个包含圆括号或方括号、不超过80个字符的表达式串。
输出格式
第一行:若输入表达式括号匹配,输出"matching"; 若不匹配,输出具体信息:"isn't matched pairs", 或"lack of left parenthesis"或"lack of right parenthesis"
输入样例
8*[3*(35-23)]
输出样例
matching
typedef char SElemType;
#include"malloc.h"
#include"stdio.h"
#include"math.h"
#include"stdlib.h" // exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
#define STACK_INIT_SIZE 10 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{
S.base=new SElemType[STACK_INIT_SIZE];
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status StackEmpty(SqStack S)
{
return (S.base==S.top);
}
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base==S.stacksize) return ERROR;
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
void check()
{ // 对于输入的任意一个字符串,检验括号是否配对
SqStack s;
SElemType ch[80],*p,e;
if(InitStack(s)) // 初始化栈成功
{
//printf("请输入表达式\n");
gets(ch);
p=ch;
while(*p) // 没到串尾
switch(*p)
{
case '(':
case '[':Push(s,*p++);
break; // 左括号入栈,且p++
case ')':
case ']':if(!StackEmpty(s)) // 栈不空
{
Pop(s,e); // 弹出栈顶元素
if(*p==')'&&e!='('||*p==']'&&e!='[')
// 弹出的栈顶元素与*p不配对
{
printf("isn't matched pairs\n");
exit(ERROR);
}
else
{
p++;
break; // 跳出switch语句
}
}
else // 栈空
{
printf("lack of left parenthesis\n");
exit(ERROR);
}
default: p++; // 其它字符不处理,指针向后移
}
if(StackEmpty(s)) // 字符串结束时栈空
printf("matching\n");
else
printf("lack of right parenthesis\n");
}
}
int main()
{
check();
}
8587.行编辑程序
Description 利用栈编写简单的行编辑程序:接受用户从终端输入的程序或数据,在输入过程中,允许用户输入出差错,并在发现有误时可以及时更正。例如:当用户发现刚刚键入的一个字符是错的时,可以补进一个退格符“#”,以表示前一个字符无效;如果发现当前键入的行内差错较多或难以补救,则可以键入一个退行符“@”,以表示当前行中的字符均无效。例如:假设从终端接受了这样两行字符: whli##ilr#e (s#*s) outcha@putchar(*s=#++); 则实际有效的是下列两行: while (*s) putchar(*s++); 本题目给出部分函数,要求将行编辑函数补充完整,并完成整个程序。
typedef char SElemType;
#include"malloc.h"
#include"stdio.h"
#include"math.h"
#include"stdlib.h" // exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
#define STACK_INIT_SIZE 10 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{ // 构造一个空栈S
}
Status StackEmpty(SqStack S)
{ // 若栈S为空栈,则返回TRUE,否则返回FALSE
}
Status ClearStack(SqStack &S)
{ // 把S置为空栈
S.top=S.base;
return OK;
}
Status DestroyStack(SqStack &S)
{ // 销毁栈S,S不再存在
free(S.base);
S.base=NULL;
S.top=NULL;
S.stacksize=0;
return OK;
}
Status Push(SqStack &S,SElemType e)
{ // 插入元素e为新的栈顶元素
}
Status Pop(SqStack &S,SElemType &e)
{ // 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
}
Status StackTraverse(SqStack S,Status(*visit)(SElemType))
{ // 从栈底到栈顶依次对栈中每个元素调用函数visit()。
// 一旦visit()失败,则操作失败
while(S.top>S.base)
visit(*S.base++);
printf("\n");
return OK;
}
Status visit(SElemType c)
{
printf("%c",c);
return OK;
}
void LineEdit()
{ // 利用字符栈s,从终端接收一行并送至调用过程的数据区。算法3.2
SqStack s;
char ch,c;
int n,i;
InitStack(s);
scanf("%d",&n);
ch=getchar();
for(i=1;i<=n;i++)
{ ch=_______________________________;
while(ch!='\n')
{
switch(_____________________)
{
case '#':Pop(s,c);
break; // 仅当栈非空时退栈
case '@':ClearStack(s);
break; // 重置s为空栈
default :_________________________________; // 有效字符进栈
}
____________________________; // 从终端接收下一个字符
}
StackTraverse(s,visit); // 将从栈底到栈顶的栈内字符输出
_____________________________________; // 重置s为空栈
}
DestroyStack(s);
}
void main()
{
LineEdit();
}
输入格式
第一行:第一个字符为输入文本的行数n;
第二行至第n行:每行均为一串字符,其间可以含有“#”和“@”符号,以回车键结束本行的输入;
输出格式
输出第一至第n行的内容如下:
第一行:第一行从终端输入的有效字符。
第二行:第二行从终端输入的有效字符。
…… ……
第n行:第n行从终端输入的有效字符。
输入样例
2
defne##ine OK 1
typp cila@type int element
输出样例
define OK 1
type int element
typedef char SElemType;
#include"cstdlib"
#include"stdio.h"
#include"math.h"
#include"stdlib.h" // exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
#define STACK_INIT_SIZE 30 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{ // 构造一个空栈S
S.base=(SElemType*)malloc(STACK_INIT_SIZE* sizeof(SElemType));
if(!S.base)
return ERROR;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status ClearStack(SqStack &S)
{ // 把S置为空栈
S.top=S.base;
return OK;
}
Status DestroyStack(SqStack &S)
{ // 销毁栈S,S不再存在
free(S.base);
S.base=NULL;
S.top=NULL;
S.stacksize=0;
return OK;
}
Status Push(SqStack &S,SElemType e)
{
// 在栈S中插入元素e为新的栈顶元素
// 请补全代码
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)malloc((STACK_INIT_SIZE+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)
return ERROR;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top=e;
S.top++;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
// 请补全代码
if(S.top==S.base)return ERROR;
e=*(S.top-1);
S.top--;
return OK;
}
Status StackTraverse(SqStack S,Status(*visit)(SElemType))
{ // 从栈底到栈顶依次对栈中每个元素调用函数visit()。
// 一旦visit()失败,则操作失败
while(S.top>S.base)
visit(*S.base++);
printf("\n");
return OK;
}
Status visit(SElemType c)
{
printf("%c",c);
return OK;
}
void LineEdit()
{ // 利用字符栈s,从终端接收一行并送至调用过程的数据区。算法3.2
//2
//
//defne##ine OK 1
//
//typp cila@type int element
SqStack s;
char ch,c;
int n,i;
InitStack(s);
scanf("%d",&n);
ch=getchar();//读换行
for(i=1;i<=n;i++)
{ ch=getchar();
while(ch!='\n')
{
switch(ch)
{
case '#':Pop(s,c);
break; // 仅当栈非空时退栈
case '@':ClearStack(s);
break; // 重置s为空栈
default :Push(s,ch); // 有效字符进栈
}
ch=getchar(); // 从终端接收下一个字符
}
StackTraverse(s,visit); // 将从栈底到栈顶的栈内字符输出
ClearStack(s); // 重置s为空栈
}
DestroyStack(s);
}
int main()
{
LineEdit();
}
8591.计算next值
Description 编写算法,录入多个字符串计算并验证NEXT值,输入0结束。本题目给出部分代码,请补全内容。]
#include "stdio.h"
#include "stdlib.h"
#include "iostream.h"
#define MAXSTRLEN 255 // 用户可在255以内定义最大串长
typedef unsigned char SString[MAXSTRLEN+1]; // 0号单元存放串的长度
void get_next(SString T,int next[]){
// 算法4.7
// 求模式串T的next函数值并存入数组next
// 请补全代码
}
void main(){
int next[MAXSTRLEN];
SString S;
int n,i,j;
char ch;
scanf("%d",&n); // 指定要验证NEXT值的字符串个数
ch=getchar();
for(i=1;i<=n;i++)
{
ch=getchar();
for(j=1;j<=MAXSTRLEN&&(ch!='\n');j++) // 录入字符串
{
S[j]=ch;
ch=getchar();
}
S[0]=j-1; // S[0]用于存储字符串中字符个数
get_next(S,next);
printf("NEXT J is:");
for(j=1;j<=S[0];j++)
printf("%d",next[j]);
printf("\n");
}
}
输入格式
第一行:输入n,表示有n个需计算NEXT值的字符串
第二至n+1行:每行输入一个字符串
输出格式
第1至第n行:通过计算每相应行的字符串得出的NEXT值
输入样例
4
abcdefg
aaaaab
abaabcac
aaabaaab
输出样例
NEXT J is:0111111
NEXT J is:012345
NEXT J is:01122312
NEXT J is:01231234
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#define MAXSTRLEN 255 // 用户可在255以内定义最大串长
typedef unsigned char SString[MAXSTRLEN+1]; // 0号单元存放串的长度
void get_next(SString T,int next[]){
// 算法4.7
// 求模式串T的next函数值并存入数组next
// 请补全代码
next[1]=0;
int i=1,j=0;
while(i<=T[0])
{
if((j==0)||T[i]==T[j])
next[++i]=++j;
else j=next[j];
}
}
int main(){
int next[MAXSTRLEN];
SString S;
int n,i,j;
char ch;
scanf("%d",&n); // 指定要验证NEXT值的字符串个数
ch=getchar();
for(i=1;i<=n;i++)
{
ch=getchar();
for(j=1;j<=MAXSTRLEN&&(ch!='\n');j++) // 录入字符串
{
S[j]=ch;
ch=getchar();
}
S[0]=j-1; // S[0]用于存储字符串中字符个数
get_next(S,next);
printf("NEXT J is:");
for(j=1;j<=S[0];j++)
printf("%d",next[j]);
printf("\n");
}
}
8592.KMP算法
Description 用KMP算法对主串和模式串进行模式匹配。本题目给出部分代码,请补全内容。
#include "stdio.h"
#include "stdlib.h"
#include "iostream.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASLBLE -1
#define OVERFLOW -2
#define MAXSTRLEN 255 //用户可在255以内定义最大串长
typedef unsigned char SString[MAXSTRLEN+1]; //0号单元存放串的长度
void get_next(SString T,int next[]){
// 算法4.7
// 求模式串T的next函数值并存入数组next
// 请补全代码
}
int Index_KMP(SString S,SString T,int pos){
// 算法4.6
// 利用模式串T的next函数求T在主串S中第pos个字符之后的位置
// KMP算法。请补全代码
}
void main()
{
SString T,S;
int i,j,n;
char ch;
int pos;
scanf(“%d”,&n); // 指定n对需进行模式匹配的字符串
ch=getchar();
for(j=1;j<=n;j++)
{
ch=getchar();
for( i=1;i<=MAXSTRLEN&&(ch!='\n');i++) // 录入主串
{
S[i]=ch;
ch=getchar();
}
S[0]=i-1; // S[0]用于存储主串中字符个数
ch=getchar();
for( i=1;i<=MAXSTRLEN&&(ch!='\n');i++) // 录入模式串
{
T[i]=ch;
ch=getchar();
}
T[0]=i-1; // T[0]用于存储模式串中字符个数
pos= ; // 请填空
printf("%d\n",pos);
}
}
输入格式
第一行:输入n,表示有n对字符串需要匹配
第二行:输入第1个主串
第三行:输入第1个模式串
第四行:输入第2个主串
第五行:输入第2个模式串
……
倒数二行:输入第n个主串
最后一行:输入第n个模式串
输出格式
第一至第n行:输出每相应模式串的匹配值
输入样例
4
oadhifgoarhglkdsa
oar
abcdefg
dec
algeojflas
ojf
jfaweiof
of
输出样例
8
0
5
7
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASLBLE -1
#define OVERFLOW -2
#define MAXSTRLEN 255 //用户可在255以内定义最大串长
typedef unsigned char SString[MAXSTRLEN+1]; //0号单元存放串的长度
void get_next(SString T,int next[]){
// 算法4.7
// 求模式串T的next函数值并存入数组next
// 请补全代码
next[1]=0;
int i=1,j=0;
while(i<=T[0])
{
if(j==0||T[i]==T[j])
next[++i]=++j;
else
j=next[j];
}
}
int Index_KMP(SString S,SString T,int pos){
// 算法4.6
// 利用模式串T的next函数求T在主串S中第pos个字符之后的位置
// KMP算法。请补全代码
int next[MAXSTRLEN+1];
get_next(T,next);
int i=pos,j=0;
while(i<=S[0]&&j<=T[0])
{
if(j==0||S[i]==T[j])
{
++i;
++j;
}
else j=next[j];
}
if(j>=T[0]) return i-T[0];
else return 0;
}
int main()
{
SString T,S;
int i,j,n;
char ch;
int pos;
scanf("%d",&n); // 指定n对需进行模式匹配的字符串
ch=getchar();
for(j=1;j<=n;j++)
{
ch=getchar();
for( i=1;i<=MAXSTRLEN&&(ch!='\n');i++) // 录入主串
{
S[i]=ch;
ch=getchar();
}
S[0]=i-1; // S[0]用于存储主串中字符个数
ch=getchar();
for( i=1;i<=MAXSTRLEN&&(ch!='\n');i++) // 录入模式串
{
T[i]=ch;
ch=getchar();
}
T[0]=i-1; // T[0]用于存储模式串中字符个数
pos= Index_KMP(S,T,0); ; // 请填空
printf("%d\n",pos);
}
}
8606.二叉树的构建及遍历操作
Description 构造二叉链表表示的二叉树:按先序次序输入二叉树中结点的值(一个字符),'#'字符表示空树,构造二叉链表表示的二叉树T;再输出三种遍历序列。本题只给出部分代码,请补全内容。
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;//左右孩子指针
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T) { // 算法6.4
// 按先序次序输入二叉树中结点的值(一个字符),’#’字符表示空树,
// 构造二叉链表表示的二叉树T。
char ch;
scanf("%c",&ch);
if (ch=='#') T = NULL;
else {
if (!(T = (BiTNode *)malloc(sizeof(BiTNode)))) return ERROR;
________________________ // 生成根结点
_______________________ // 构造左子树
_________________________ // 构造右子树
}
return OK;
} // CreateBiTree
Status PreOrderTraverse( BiTree T) {
// 前序遍历二叉树T的递归算法
//补全代码,可用多个语句
} // PreOrderTraverse
Status InOrderTraverse( BiTree T) {
// 中序遍历二叉树T的递归算法
//补全代码,可用多个语句
} // InOrderTraverse
Status PostOrderTraverse( BiTree T) {
// 后序遍历二叉树T的递归算法
//补全代码,可用多个语句
} // PostOrderTraverse
int main() //主函数
{
//补充代码
}//main
输入格式
第一行:输入一棵二叉树的先序遍历序列
输出格式
第一行:二叉树的先序遍历序列
第二行:二叉树的中序遍历序列
第三行:二叉树的后序遍历序列
输入样例
AB##C##
输出样例
ABC
BAC
BCA
#include<iostream>
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
using namespace std;
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;//左右孩子指针
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T) { // 算法6.4
// 按先序次序输入二叉树中结点的值(一个字符),’#’字符表示空树,
// 构造二叉链表表示的二叉树T。
char ch;
scanf("%c",&ch);
if (ch=='#') T = NULL;
else {
if (!(T = (BiTNode *)malloc(sizeof(BiTNode)))) return ERROR;
T->data=ch; // 生成根结点
CreateBiTree(T->lchild); // 构造左子树
CreateBiTree(T->rchild); // 构造右子树
}
return OK;
} // CreateBiTree
Status PreOrderTraverse( BiTree T) {
// 前序遍历二叉树T的递归算法
//补全代码,可用多个语句
if(T)
{
cout<<T->data;
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
return OK;
}
} // PreOrderTraverse
Status InOrderTraverse( BiTree T) {
// 中序遍历二叉树T的递归算法
//补全代码,可用多个语句
if(T)
{
InOrderTraverse(T->lchild);
cout<<T->data;
InOrderTraverse(T->rchild);
return OK;
}
} // InOrderTraverse
Status PostOrderTraverse( BiTree T) {
// 后序遍历二叉树T的递归算法
//补全代码,可用多个语句
if(T)
{
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
cout<<T->data;
return OK;
}
} // PostOrderTraverse
int main() //主函数
{
//补充代码
BiTree T;
CreateBiTree(T);
PreOrderTraverse(T);
cout<<endl;
InOrderTraverse(T);
cout<<endl;
PostOrderTraverse(T);
cout<<endl;
return 0;
}//main
17121.求二叉树的各种节点数
Description
构造二叉链表表示的二叉树:按先序次序输入二叉树中结点的值(一个字符),'#'字符表示空树,构造二叉链表表示的二叉树T,并求此二叉树中度为2的节点总数,度为1的节点总数,度为0的节点总数
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;//左右孩子指针
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T) { // 算法6.4
// 按先序次序输入二叉树中结点的值(一个字符),’#’字符表示空树,
// 构造二叉链表表示的二叉树T。
char ch;
scanf("%c",&ch);
if (ch=='#') T = NULL;
else {
if (!(T = (BiTNode *)malloc(sizeof(BiTNode)))) return ERROR;
________________________ // 生成根结点
_______________________ // 构造左子树
_________________________ // 构造右子树
}
return OK;
} // CreateBiTree
int main() //主函数
{
//补充代码
}//main
输入格式
第一行输入先序次序二叉树中结点
输出格式
第一行输出度为2的节点数
第二行输出度为1的节点数
第三行输出度为0的节点数
输入样例
ABC###D##
输出样例
1
1
2
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;//左右孩子指针
} BiTNode,*BiTree;
int n0=0,n=0;
Status CreateBiTree(BiTree &T) { // 算法6.4
// 按先序次序输入二叉树中结点的值(一个字符),’#’字符表示空树,
// 构造二叉链表表示的二叉树T。
char ch;
scanf("%c",&ch);
if (ch=='#') T = NULL;
else {
if (!(T = (BiTNode *)malloc(sizeof(BiTNode)))) return ERROR;
T->data=ch; // 生成根结点
n++;
CreateBiTree(T->lchild);// 构造左子树
CreateBiTree(T->rchild); }// 构造右子树
return OK;
} // CreateBiTree
void f(BiTree &T)
{
if(!T) return ;
if(!T->lchild&&!T->rchild)n0++;
f(T->lchild);
f(T->rchild);
}
int main() //主函数
{
BiTree T;
CreateBiTree(T);
f(T);
printf("%d\n%d\n%d\n",n0-1,n-2*n0+1,n0);
}
18924二叉树的宽度
18924 二叉树的宽度
时间限制:1000MS 代码长度限制:10KB
提交次数:0 通过次数:0
题型: 编程题 语言: G++;GCC
Description
二叉树的宽度指的是具有节点数目最多的那一层的节点个数。
1
/ \
2 3
/
4
答案为2, 第二层节点数最多,为2个节点。
输入格式
共n行。
第一行一个整数n,表示有n个结点,编号为1至n,结点1为树根。(1<=n<=50)
第二行至第n行,每行有两个整数x和y,表示在二叉树中x为y的父节点。x第一次出现时y为左孩子
输出格式
输出二叉树的宽度。
输入样例
5
1 2
1 3
2 4
2 5
输出样例
2
#include<iostream>
#include<queue>
using namespace std;
int n,child[105][3],ans=1;
int main()
{
int x,y,i,j;
cin>>n;
for(i=1;i<n;i++)
{
cin>>x>>y;
if(!child[x][1])
child[x][1]=y;
else
child[x][2]=y;
}
queue <int> q;
q.push(1);
while(q.size())
{
int len=q.size();
ans=max(ans,len);
for(i=0;i<len;i++)
{
int t=q.front();
q.pop();
if(child[t][1])
q.push(child[t][1]);
if(child[t][2])
q.push(child[t][2]);
}
}
cout<<ans;
return 0;
}
8610顺序查找
8610 顺序查找
时间限制:1000MS 代码长度限制:10KB
提交次数:2303 通过次数:1423
题型: 编程题 语言: G++;GCC
Description 编写Search_Seq函数,实现在一个无序表ST中采用顺序查找算法查找值为key的元素的算法.
#include"malloc.h" /* malloc()等 */
#include"stdio.h"
#include"stdlib.h"
typedef int ElemType;
typedef struct /*静态查找表的顺序存储结构 */
{
ElemType *elem; /* 数据元素存储空间基址,建表时按实际长度分配,0号单元留空 */
int length; /* 表长度 */
}SSTable;
void Creat_Seq(SSTable &ST,int n)
{ /* 操作结果: 构造一个含n个数据元素的静态顺序查找表ST(数据来自数组r) */
int i,temp;
ST.elem=(ElemType *)malloc((n+1) * sizeof(ElemType)); /* 动态生成n个数据元素空间(0号单元不用) */
if(!(ST).elem)
{
printf("ERROR\n");
exit(0);
} /*内存分配失败结束程序*/
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
*(ST.elem+i)=temp; /* 依次赋值给ST */
}
ST.length=n;
}
int Search_Seq(SSTable &ST,ElemType key)
{ /* 在顺序表ST中顺序查找其关键字等于key的数据元素。若找到,则函数值为 */
/* 该元素在表中的位置,否则为0。算法9.1 */
}
main()
{
SSTable ST;
int loc,key;
int n;
scanf("%d",&n);
Creat_Seq(ST,n);
//printf("Please input the key value:");
scanf("%d",&key);
loc = Search_Seq(ST,key);
if(loc!=0)
printf("The element position is %d.\n",loc);
else
printf("The element is not exist.\n");
}
输入格式
第一行:元素个数n
第二行:依次输入n个元素的值
第三行:输入要查找的关键字key的值
输出格式
输出分两种情形:
1.如果key值存在,则输出其在表中的位置x(表位置从1开始),格式为The element position is x.
2.如果key值不存在输出:"The element is not exist."
输入样例
6
1 3 5 7 9 10
5
#include"malloc.h" /* malloc()等 */
#include"stdio.h"
#include"stdlib.h"
typedef int ElemType;
typedef struct /*静态查找表的顺序存储结构 */
{
ElemType *elem; /* 数据元素存储空间基址,建表时按实际长度分配,0号单元留空 */
int length; /* 表长度 */
}SSTable;
void Creat_Seq(SSTable &ST,int n)
{ /* 操作结果: 构造一个含n个数据元素的静态顺序查找表ST(数据来自数组r) */
int i,temp;
ST.elem=(ElemType *)malloc((n+1) * sizeof(ElemType)); /* 动态生成n个数据元素空间(0号单元不用) */
if(!(ST).elem)
{
printf("ERROR\n");
exit(0);
} /*内存分配失败结束程序*/
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
*(ST.elem+i)=temp; /* 依次赋值给ST */
}
ST.length=n;
}
int Search_Seq(SSTable &ST,ElemType key)
{ /* 在顺序表ST中顺序查找其关键字等于key的数据元素。若找到,则函数值为 */
/* 该元素在表中的位置,否则为0。算法9.1 */
for(int i=1;i<=ST.length;i++)
if(ST.elem[i]==key)
return i;
return 0;
}
int main()
{
SSTable ST;
int loc,key;
int n;
scanf("%d",&n);
Creat_Seq(ST,n);
//printf("Please input the key value:");
scanf("%d",&key);
loc = Search_Seq(ST,key);
if(loc!=0)
printf("The element position is %d.\n",loc);
else
printf("The element is not exist.\n");
}
8621二分查找
8621 二分查找
时间限制:1000MS 代码长度限制:10KB
提交次数:4655 通过次数:1446
题型: 编程题 语言: G++;GCC
Description 编写Search_Bin函数,实现在一个递增有序数组ST中采用折半查找法确定元素位置的算法.
输入格式
第一行:元素个数n
第二行:依次输入n个元素的值(有序)
第三行:输入要查找的关键字key的值
输出格式
输出分两种情形:
1.如果key值存在,则输出其在表中的位置x(表位置从0开始),格式为The element position is x.
2.如果key值不存在输出:"The element is not exist."
输入样例
6
1 3 5 7 9 10
5
输出样例
The element position is 2.
#include<iostream>
using namespace std;
int main()
{
int n,i;cin>>n;
int a[n];
for(i=0;i<n;i++)
cin>>a[i];
int key;cin>>key;
int low=0,high=n-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(key>a[mid]) low=mid+1;//往右边找
else if(key<a[mid]) high=mid-1;//往左边找
else
{
cout<<"The element position is "<<mid<<".";
return 0;
}
}
cout<<"The element is not exist.";
return 0;
}
8638直接插入排序
8638 直接插入排序
时间限制:1000MS 代码长度限制:10KB
提交次数:2050 通过次数:1393
题型: 编程题 语言: G++;GCC
Description
用函数实现直接插入排序,并输出每趟排序的结果.
输入格式
第一行:键盘输入待排序关键的个数n
第二行:输入n个待排序关键字,用空格分隔数据
输出格式
每行输出一趟排序结果,数据之间用一个空格分隔
输入样例
10
5 4 8 0 9 3 2 6 7 1
输出样例
4 5 8 0 9 3 2 6 7 1
4 5 8 0 9 3 2 6 7 1
0 4 5 8 9 3 2 6 7 1
0 4 5 8 9 3 2 6 7 1
0 3 4 5 8 9 2 6 7 1
0 2 3 4 5 8 9 6 7 1
0 2 3 4 5 6 8 9 7 1
0 2 3 4 5 6 7 8 9 1
0 1 2 3 4 5 6 7 8 9
#include<iostream>
using namespace std;
int n,a[10005];
void print()
{
for(int i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
void InsertSort(int a[])
{
int i,j;
for(i=2;i<=n;i++)
{
if(a[i]<a[i-1])
{
int temp=a[i];
for(j=i-1;temp<a[j];j--)
a[j+1]=a[j];
a[++j]=temp;
}
print();
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
InsertSort(a);
return 0;
}
8639折半插入排序
8639 折半插入排序
时间限制:1000MS 代码长度限制:10KB
提交次数:1738 通过次数:1371
题型: 编程题 语言: G++;GCC
Description
用函数实现折半插入排序,并输出每趟排序的结果.
输入格式
第一行:键盘输入待排序关键的个数n
第二行:输入n个待排序关键字,用空格分隔数据
输出格式
每行输出一趟排序结果,数据之间用一个空格分隔
输入样例
10
5 4 8 0 9 3 2 6 7 1
输出样例
4 5 8 0 9 3 2 6 7 1
4 5 8 0 9 3 2 6 7 1
0 4 5 8 9 3 2 6 7 1
0 4 5 8 9 3 2 6 7 1
0 3 4 5 8 9 2 6 7 1
0 2 3 4 5 8 9 6 7 1
0 2 3 4 5 6 8 9 7 1
0 2 3 4 5 6 7 8 9 1
0 1 2 3 4 5 6 7 8 9
#include<iostream>
using namespace std;
int n,a[10005];
void print()
{
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
void BiSort(int a[],int n)
{
int i,j,low,high,mid,temp;
for(i=1;i<n;i++)
{
low=0;high=i-1;
temp=a[i];
while(low<=high)
{
mid=(high+low)/2;
if(a[mid]>temp)
high=mid-1;
else
low=mid+1;
}
for(j=i;j>low;j--)
a[j]=a[j-1];
a[low]=temp;
print();
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
BiSort(a,n);
return 0;
}
8640希尔排序
8640 希尔(shell)排序
时间限制:1000MS 代码长度限制:10KB
提交次数:1858 通过次数:1304
题型: 编程题 语言: G++;GCC
Description
用函数实现希尔(shell)排序,并输出每趟排序的结果,初始增量d=n/2,其后d=d/2
输入格式
第一行:键盘输入待排序关键的个数n
第二行:输入n个待排序关键字,用空格分隔数据
输出格式
每行输出一趟排序结果,数据之间用一个空格分隔
输入样例
10
5 4 8 0 9 3 2 6 7 1
输出样例
3 2 6 0 1 5 4 8 7 9
1 0 3 2 4 5 6 8 7 9
0 1 2 3 4 5 6 7 8 9
#include<iostream>
using namespace std;
int n,a[10005];
void ShellSort(int a[],int n)
{
int d=n/2, i,j,temp;
while(d)
{
for(i=d;i<n;i++)
{
temp=a[i];
j=i;
while(j>=d&&temp<a[j-d])
{
a[j]=a[j-d];
j-=d;
}
a[j]=temp;
}
d/=2;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
ShellSort(a,n);
return 0;
}
8641冒泡排序
8641 冒泡排序
时间限制:1000MS 代码长度限制:10KB
提交次数:3093 通过次数:1361
题型: 编程题 语言: G++;GCC
Description
用函数实现冒泡排序,并输出每趟排序的结果(要求当一趟冒泡过程中不再有数据交换,则排序结束)
输入格式
第一行:键盘输入待排序关键的个数n
第二行:输入n个待排序关键字,用空格分隔数据
输出格式
每行输出每趟排序结果,数据之间用一个空格分隔
输入样例
10
5 4 8 0 9 3 2 6 7 1
输出样例
4 5 0 8 3 2 6 7 1 9
4 0 5 3 2 6 7 1 8 9
0 4 3 2 5 6 1 7 8 9
0 3 2 4 5 1 6 7 8 9
0 2 3 4 1 5 6 7 8 9
0 2 3 1 4 5 6 7 8 9
0 2 1 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
#include <iostream>
using namespace std;
void print(int a[],int n)
{
for(int k=0;k<n;k++)
cout<<a[k]<<" ";
cout<<endl;
}
void BubbleSort(int a[], int n)
{
int temp=0,i=0,j=0,k=1;
for(i=0;i<n-1;i++)
{
k=0;
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
k=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
if(k)
print(a,n);
}
print(a,n);
}
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
BubbleSort(a,n);
return 0;
}
8643简单选择排序
8643 简单选择排序
时间限制:1000MS 代码长度限制:10KB
提交次数:2235 通过次数:1301
题型: 编程题 语言: G++;GCC
Description
用函数实现简单选择排序,并输出每趟排序的结果
输入格式
第一行:键盘输入待排序关键的个数n
第二行:输入n个待排序关键字,用空格分隔数据
输出格式
每行输出每趟排序的结果,数据之间用一个空格分隔
输入样例
10
5 4 8 0 9 3 2 6 7 1
输出样例
0 4 8 5 9 3 2 6 7 1
0 1 8 5 9 3 2 6 7 4
0 1 2 5 9 3 8 6 7 4
0 1 2 3 9 5 8 6 7 4
0 1 2 3 4 5 8 6 7 9
0 1 2 3 4 5 8 6 7 9
0 1 2 3 4 5 6 8 7 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
#include<iostream>
using namespace std;
int n,a[10005];
void SimpleSort(int a[],int n)
{
int i,j,t,k;
for(i=1;i<n;i++)
{
t=i;
for(j=i+1;j<=n;j++)
{
if(a[j]<a[t])
t=j;
}
if(t!=i) swap(a[t],a[i]);
for(k=1;k<=n;k++)
cout<<a[k]<<" ";
cout<<endl;
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
SimpleSort(a,n);
return 0;
}
8647实现图的存储结构
8647 实现图的存储结构
时间限制:1000MS 代码长度限制:10KB
提交次数:1499 通过次数:1092
题型: 编程题 语言: G++;GCC
Description
实现有向图的邻接矩阵存储结构。
输入格式
第一行:输入图的顶点个数n(各个顶点的默认编号为1~n), 边的条数m。
第二 ~ m+1行:每行输入两个顶点编号i、j,表示连接顶点i到顶点j的一条边。
输出格式
分n行输出n*n的邻接矩阵,表示所输入的图存储,顶点i和顶点j之间如果有边相连,则输出1,没边相连则输出0。
输入样例
4 4
1 2
1 3
3 4
4 1
输出样例
0 1 1 0
0 0 0 0
0 0 0 1
1 0 0 0
#include<iostream>
using namespace std;
int juzhen[100][100]={0};
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int v1,v2;
cin>>v1>>v2;
juzhen[v1][v2]=1;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<juzhen[i][j]<<" ";
cout<<endl;
}
}