SWUST数据结构下半期实验练习题

news2025/5/19 16:32:24

1068: 图的按录入顺序深度优先搜索

#include"iostream"
using namespace std;
#include"cstring"
int visited[100];
char s[100];
int a[100][100];
int n;
void dfs(int k,int n)
{
	if(visited[k]==0)
	{
		visited[k]=1;
		cout<<s[k];
		for(int i=0;i<n;i++)
		{
			if(visited[i]==0&&a[k][i]!=0)
			{
				dfs(i,n);
			}
		}
	}
}
int main()
{
	cin>>n;
	cin>>s;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
	char key=0;
	cin>>key;
	for(int i=0;i<n;i++)
	{
		if(s[i]==key)
		{
			dfs(i,n);
		}
	}
}

1069: 图的按录入顺序广度优先搜索

#include"iostream"
#include"cstring"
using namespace std;
int queue[100];
int visited[100];
char s[100];
int a[100][100];
void bfs(int k,int n)
{
	int rear=-1,front=-1;
	queue[++rear]=k;
	visited[k]=1;
	while(front!=rear)
	{
		k=queue[++front];
		cout<<s[k];
		for(int i=0;i<n;i++)
		{
			if(a[k][i]!=0&&visited[i]==0)
			{
				queue[++rear]=i;
				visited[i]=1;
			}
		}
	}
}
int main() {
    int n;
    cin >> n;
    cin >> s;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> a[i][j];
        }
    }

    char key;
    cin >> key;

    for (int i = 0; i < n; i++) {
        if (s[i] == key) {
         
            bfs(i, n);
        }
    }

    return 0;
}

1070: 邻接矩阵存储简单路径

#include"iostream"
using namespace std;
#include"cstring"
int n;
int start,last;
int a[100][100];
int stu[100];
int path[100];
void dfs(int u,int t)
{
	path[t]=u;
	
	if(u==last)
	{
		for(int i=0;i<t;i++)
		{
			cout<<path[i];
		}
		cout<<last<<endl;
		return;
	}
	
	stu[u]=1;
	
	for(int i=0;i<n;i++)
	{
		if(stu[i]==0&&a[u][i]==1)
		{
			dfs(i,t+1);
		}
	}
	
	stu[u]=0;
}
int main()
{
	cin>>n;
	cin>>start>>last;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}	
	}
	dfs(start,0);
}

1055: 邻接矩阵到邻接表

#include"iostream"
using namespace std;
#include"cstring"
int n;
int a[100][100];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(a[i][j]==1)
			{
				cout<<j;
			}
		}
		cout<<endl;
	}
}

1056: 邻接表到邻接矩阵

#include"iostream"
using namespace std;
#include"cstring"
#include"stdio.h"
int n;
int a[100][100];
int x;

int main()
{
	scanf("%d",&n);
	getchar();
	for(int i=0;i<n;i++)
	{
		for(int j=0;;j++)
		{
			scanf("%c",&x);
			if(x=='\n') break;
			a[i][x-'0']=1;
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			printf("%d",a[i][j]);//写成cout超时
		}
		cout<<endl;
	}
}

1057: 有向图的出度计算

#include"iostream"
using namespace std;
#include"cstring"
#include"stdio.h"
int n,e;
int a[100][100];
int b[100];
int start,last;
int main()
{
	cin>>n>>e;
	for(int i=0;i<e;i++)
	{
		cin>>start>>last;
		a[start][last]=1;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(a[i][j]==1)
			{
				b[i]++;
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		cout<<b[i]<<endl;
	}
}

1060: 无向图的最大度计算

#include"iostream"
using namespace std;
#include"cstring"
#include"stdio.h"
int n;
int a[100][100];
int mx;
int b[100];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
	
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(a[i][j]==1)
			{
				b[i]++;
			}
			if(b[i]>mx)
			{
				mx=b[i];
			}
		}
	}
	cout<<mx<<endl;
	for(int i=0;i<n;i++)
	{
		if(b[i]==mx)
		{
			cout<<i;
		}
	}
} 

1062: 有向图的边存在判断

#include"iostream"
using namespace std;
#include"cstring"
#include"stdio.h"
int n;
int start,last;
int a[100][100];
int main()
{
	cin>>n;
	cin>>start>>last;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
	
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(a[start][last]==1)
			{
				cout<<"yes";
				return 0;
			}
		}
	}
	cout<<"no";
	
}

1065: 无向图的连通分量计算

#include"iostream"
using namespace std;
#include"cstring"
#include"stdio.h"
int n;
int a[100][100];
int separte;

int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
	int sum=1;
	for(int i=0;i<n;i++)
	{
		int separte=0;
		for(int j=0;j<n;j++)
		{
			if(a[i][j]==1)
			{
				separte=1;
				break;
			}
		}
		if(separte==0) sum++;
	}
	cout<<sum;
}

1076: 判断给定有向图是否存在回路

#include"iostream"
using namespace std;
int n;
int e;
char s[100];
char str,last;
int visited[100];
int flag;
int a[100][100];
void bfs(int j,int start)
{
	if(visited[j]==0)
	{
		if(j==start)
		{
			flag=1;
			return;
		}
		visited[j]=1;
		
		for(int i=0;i<n;i++)
		{
			if(a[j][i]!=0&&visited[i]==0)
			{
				bfs(i,start);
			}
		}visited[j]=0;
	}
	
}
int main()
{
	cin>>n;
	cin>>e;
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
	}
	for(int i=0;i<e;i++)
	{
		cin>>str>>last;
		for(int j=0;j<n;j++)
		{
			if(s[j]==str)
			{
				for(int k=0;k<n;k++)
				{
					if(s[k]==last)
					{
						a[j][k]=1;					
					}
				}
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(a[i][j]!=0)
			{
				bfs(j,i);
			}
		}
	}
	if(flag==1) cout<<"yes";
	else cout<<"no";
}

1075: 求最小生成树(Prim算法)

#include<iostream> 
using namespace std; 
const int maxn = 100;
const int INF = 101;
typedef struct 
{ 
int n; 
int e; 
char data[500]; 
int edge[500][500]; 
}Graph; 

typedef struct 
{ 
int index; 
int cost; 
}mincost; 

typedef struct 
{ 
int x; 
int y; 
int weight;    
}EDGE; 


typedef struct 
{ 
int index; 
int flag; 
}F; 

void create(Graph &G,int n ,int e) 
{ 
int i,j,k,w; 
char a,b; 
for(i=0;i< n;i++) 
cin>>G.data[i]; 
for(i=0;i< n;i++) 
for(j=0;j< n;j++) 
{ 
if(i==j) 
G.edge[i][j]=0; 
else 
G.edge[i][j]=100; 
} 

for(k=0;k< e;k++) 
{ 
cin>>a; 
cin>>b; 
cin>>w; 
for(i=0;i< n;i++) 
if(G.data[i]==a) break; 
for(j=0;j< n;j++) 
if(G.data[j]==b) break; 

G.edge[i][j]=w; 
G.edge[j][i]=w; 
} 
G.n=n; 
G.e=e; 
} 


void Prim(Graph& G,int k)
{
	int pe[maxn];
	int pn[maxn];
	int MIN;
	int v;
	for(int i=0;i<G.n;i++)
	{
		pe[i]=G.edge[k][i];
		pn[i]=k;
	}
	for(int i=1;i<G.n;i++)
	{
		MIN=INF;
		for(int j=0;j<G.n;j++)
		{
			if(pe[j]!=0&&pe[j]<MIN)
			{
				MIN=pe[j];
				v=j;
			}
		}
		cout<<"("<<G.data[pn[v]]<<','<<G.data[v]<<")";
		pe[v]=0;
		for(int j=0;j<G.n;j++)
		{
			if(pe[j]!=0&&G.edge[v][j]<pe[j])
			{
				pe[j]=G.edge[v][j];
				pn[j]=v;
			}
		}
	}
}



int main() 
{ 
Graph my; 
int n,e; 
cin>>n>>e; 
create(my,n,e); 
Prim(my,0);    
return 0; 
} 

1067: 有向图的邻接表存储强连通判断

#include"iostream"
using namespace std;
int n;
int e;
int str;
int last;
int a[100][100];
int main()
{
	cin>>n;
	cin>>e;
	for(int i=0;i<e;i++)
	{
		cin>>str>>last;
		a[str][last]=1;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			for(int k=0;k<n;k++)
			{
				if(a[j][i]==1&&a[i][k]==1)
				{
					a[j][k]=1;
				}
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
		
		if(a[i][j]==0)
		{
			cout<<"no";
			return 0;
		}
		}
	}
	cout<<"yes";
}

1012: 哈希表(链地址法处理冲突)

#include"iostream"
using namespace std;
int n;
int m;
int a[100][100];
int b[100];
int key;
int data;
int main()
{
	cin>>m;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>data;
		a[data%m][++(b[data%m])]=data;
	}
	int cnt=0;
	cin>>key;
	for(int i=0;i<n;i++)
	{
		if(a[key%m][i]==key)
		{
			cout<<key%m<<','<<cnt;
			return 0;
		}
		else
		{
			cnt++;
		}
	}
	cout<<"-1";
}

1013: 哈希表(开放定址法处理冲突)

#include"iostream"
#include"cstring"
using namespace std;
const int N=200003,null=0x3f3f3f3f;
int h[N];
int n;
int m;
int a[100];
int key;
int cnt;
int find(int x,int size)
{
	cnt=1;
	int t=x%size;
	while(h[t]!=x&&h[t]!=null)
	{
		cnt++;
		t++;
		if(t==size) t=0;
	}
	return t;
}
int main()
{
	
	memset(h,0x3f,sizeof h);
	cin>>n;
	int data=0;
	cin>>m;	
	for(int i=0;i<m;i++)
	{
		cin>>data;
		h[find(data,n)]=data;
	}
	cin>>key;
	if(h[find(key,n)]==null) cout<<"-1";
	else cout<<find(key,n)<<','<<cnt;
	
}

1011: 二叉排序树的实现和查找

#include"iostream"
using namespace std;
int a[100];
int n;
typedef struct tree
{
	struct tree* left,*right;
	char data;
}tree;
tree* buynode(char ch)
{
	tree* root=(tree*)malloc(sizeof(tree));
	root->data=ch;
	root->left=root->right=NULL;
	return root;
}
tree* insert(tree* root,int x)
{
	if(root==NULL)
	{
		root=buynode(x);
		return root;
	}
	if(root->data<x)
	{
		root->right=insert(root->right,x);
	}
	if(root->data>x)
	{
		root->left=insert(root->left,x);
	}
	return root;
}
void create(tree*& root,int a[])
{
	root=NULL;
	for(int i=0;i<n;i++)
	{
		root=insert(root,a[i]);
	}
}
int cnt=0;
tree* find(tree* root,int key)
{
	if(root==NULL) return NULL;
	if(key<root->data)
	{
		cnt++;
		return find(root->left,key);
	}
	if(root->data<key)
	{
		cnt++;
		return find(root->right,key);
	}
	if(root->data==key)
	{
		return root;
	}
}
int key;
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	cin>>key;
	tree* root=NULL;
	create(root,a);
	if(find(root,key)==NULL) cout<<"-1";
	else cout<<cnt+1;
}

1016: 插入排序算法实现

#include"iostream"
using namespace std;
int n;
int x;
int b[100];
int a[100];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(int i=1;i<n;i++)
	{
		int end=i;
		int tmp=a[end];
		while(end-1>=0)
		{
			if(a[end-1]>tmp)
			{
				a[end]=a[end-1];
				end-=1;
			}
		}
		a[end]=tmp;
		break;
	}
	for(int i=0;i<n;i++)
	{
		cout<<a[i]<<" ";
	}
}

1099: 希尔排序算法实现

#include"iostream"
using namespace std;
char s[100];
#include"cstring"
int n;
int a[100];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	int gap=5;
	for(int i=0;i<gap;i++)
	{
		int end=i;
		int tmp=a[end+gap];
		while(end>=0)
		{
			if(a[end]>tmp)
			{
				a[end+gap] =a[end];
				end-=gap;
			}
		}
		a[end+gap]=tmp;
	}
	for(int i=0;i<n;i++)
	{
		cout<<a[i]<<" ";
	}
	
}

980: 输出利用先序遍历创建的二叉树的层次遍历序列

#include"iostream"
#include"queue"
using namespace std;
typedef struct node
{
	struct node* left,*right;
	char data;
}node,*tree;
tree create()
{
	tree t;
	char ch;
	cin>>ch;
	if(ch=='#')
	{
		return NULL;
	}
	else
	{
		t=new node;
		t->data=ch;
		t->left=create();
		t->right=create();
		
	}
	return t;
}
void f(tree t)
{
	queue<tree>q;
	q.push(t);
	tree p;
	p=q.front();
	while(!q.empty())
	{
		cout<<q.front()->data;
		p=q.front();
		q.pop();
		if(p->left!=NULL)
		{
			q.push(p->left);
		}
		if(p->right!=NULL)
		{
			q.push(p->right);
		}
		
	}
}
int main()
{
	tree t=create();
	f(t);
}

987: 输出用先序遍历创建的二叉树是否为完全二叉树的判定结果

#include"iostream"
using namespace std;
char s[100];
#include"queue"
typedef struct node
{
	struct node*left,*right;
	char data;
}node,*tree;
tree create()
{
	tree t;
	char ch;
	cin>>ch;
	if(ch=='#')
	{
		return NULL;
	}
	else
	{
		t=new node;
		t->data=ch;
		t->left=create();
		t->right=create();
		
	}
	return t;
}
bool f(tree t)
{
	queue<tree>q;
	tree p;
	q.push(t);
	p=q.front();
	int flag=0;
	while(!q.empty())
	{
		
	
		p=q.front();
		q.pop();
		if(p->left!=NULL)
		{
			q.push(p->left);
		}
		else
		{
			flag=1;
		}
		if(p->right!=NULL&&flag==0)
		{
			q.push(p->right);
		}
		else
		{
			return 0;
		}
	}
	return 1;
	
}
int main()
{
	tree t=create();
	if(f(t)==1) cout<<"Y";
	else cout<<"N";
}

1098: 堆的判断

#include"iostream"
using namespace std;
char s[100];
#include"cstring"
int n;
int a[100];
int judge(int parent,int n)
{
	int child=parent*2;
	if(child<=n)
	{
		if(child<n)
		{
			if(a[child+1]<a[child])
			child++;
		}
		if(a[parent]<a[child]) 
		return 1;
		else return 0;
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(int i=n/2;i>=1;i--)
	{
		if(judge(i,n)==0)
		{
			cout<<"No";
			return 0;
		}
	}
	cout<<"Yes";
}

1015: 堆排序算法

#include"iostream"
using namespace std;
int a[100];
int n;
void sort(int parent,int n)
{
	int child=parent*2;
	int top=a[parent];
	while(child<=n)
	{
		if(child<n)
		{
			if(a[child+1]<a[child])
			{
				child++;
			}
		}
		if(a[child]<top)
		{
			a[parent]=a[child];
			parent=child;
			child=parent*2;
		}
	}
	a[parent]=top;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(int i=n/2;i>=1;i--)
	{
		sort(i,n);
	}
	for(int i=1;i<=n;i++)
	{
		cout<<a[i]<<" ";
	}
}

981: 统计利用二叉树存储的森林中树的棵数

#include"iostream"
#include"queue"
using namespace std;
typedef struct tree
{
	struct tree* left,*right;
	char data;
}tree;
tree* buynode(char ch)
{
	tree* root=(tree*)malloc(sizeof(tree));
	root->data=ch;
	root->left=root->right=NULL;
	return root;
}
tree* create(char s[],int* data)
{
	if(s[*data]=='#'||s[*data]=='\0')
	{
		(*data)++;
		return NULL;
	}
	tree* root=buynode(s[*data]);
	(*data)++;
	root->left=create(s,data);
	root->right=create(s,data);
	return root;
}
int forest(tree* root)
{
	int count=0;
	if(root==NULL) return 0;
	if(root->right==NULL) return 0;
	if(root->right!=NULL) count=1;
	return forest(root->right)+count;
}
int main()
{
	char s[100];
	while(scanf("%s",s)!=EOF)
	{
		int data=0;
		tree* root=create(s,&data);
		cout<<forest(root)+1;
	}
}

982: 输出利用二叉树存储的普通树的度

#include"iostream"
#include"queue"
using namespace std;
typedef struct tree
{
	struct tree* left,*right;
	char data;
}tree;
tree* buynode(char ch)
{
	tree* root=(tree*)malloc(sizeof(tree));
	root->data=ch;
	root->left=root->right=NULL;
	return root;
}
tree* create(char s[],int*data)
{
	if(s[*data]=='#'||s[*data]=='\0')
	{
		(*data)++;
		return NULL;
	}
	tree* root=buynode(s[*data]);
	(*data)++;
	root->left=create(s,data);
	root->right=create(s,data);
	return root;
}
int lf(tree* root)
{
	int count=0;
	if(root==NULL) return NULL;
	if(root->right!=NULL) count=1;
	return lf(root->right)+count;
	
}
int forest(tree* root)
{
	if(root==NULL) return 0;
	if(root->right!=NULL) return 0;
	tree* lrof=root->left;
	lf(lrof);
	return lf(lrof)+1;
}
int main()
{
	char s[100];
	while(scanf("%s",s)!=EOF)
	{
		int data=0;
		tree* root=create(s,&data);
		if(forest(root)==0) cout<<"ERROR";
		else cout<<forest(root);
	}
}

984: 利用二叉树中序及先序遍历确定该二叉树的后序序列

#include"iostream"
#include"queue"
#include"string.h"
using namespace std;
typedef struct tree
{
	struct tree* left,*right;
	char data;
}tree;
tree* buynode(char ch)
{
	tree* root=(tree*)malloc(sizeof(tree));
	root->data=ch;
	root->left=root->right=NULL;
	return root;
}
tree* later(char* prder,char* order,int x)
{ 
	if(x==0) return NULL;
	char *p=NULL;
	int t=0;
	for(p=order;p<order+x;p++)
	{
		if(*p==prder[0])
		{
			break;
		}
		else
		{
			t++;
		}
	}
	tree* root=buynode(prder[0]);
	root->left=later(prder+1,order,t);
	root->right=later(prder+1+t,order+t+1,x-t-1);
	return root;
}
void print(tree* root)
{
	if(root!=NULL)
	{
		print(root->left);
		print(root->right);
		cout<<root->data;
	}
}
int main()
{
	char order[100];
	char prder[100];
	cin>>order;
	cin>>prder;
	tree*root=later(prder,order,strlen(prder));
	print(root);
}

986: 哈夫曼译码

const int maxvalue=100;

const int maxbit=100;

const int maxn=100;

#include "iostream"

#include "stdio.h"

#include "stdlib.h"

using namespace std;

struct haffnode

{

char ch;

int weight;

int flag;

int parent;

int leftchild;

int rightchild;

};

struct code

{

int bit[maxn];

int start;

int weight;

char ch;

};

void haffman(int weight[],char text[],int n,haffnode hafftree[])

{

int j,m1,m2,x1,x2,i;

for(i=0;i< 2*n-1;i++)

{

if(i < n)

{

hafftree[i].weight=weight[i];

hafftree[i].

ch=text[i];

}

else

{

hafftree[i].weight=0;

hafftree[i].ch='#';

}

hafftree[i].parent=0;

hafftree[i].flag=0;

hafftree[i].leftchild=-1;

hafftree[i].rightchild=-1;

}

for(i=0;i< n-1;i++)

{

m1=m2=maxvalue;

x1=x2=0;

for(j=0;j< n+i;j++)

{

if(hafftree[j].weight< m1&&hafftree[j].flag==0)

{

m2=m1;

x2=x1;

m1=hafftree[j].weight;

x1=j;

}

else if(hafftree[j].weight< m2&&hafftree[j].flag==0)

{

m2=hafftree[j].weight; x2=j;

}

}

hafftree[x1].parent=n+i;

hafftree[x2].parent=n+i;

hafftree[x1].flag=1;

hafftree[x2].flag=1;

hafftree[n+i].weight=hafftree[x1].weight+hafftree[x2].weight;

hafftree[n+i].leftchild=x1; hafftree[n+i].rightchild=x2;

}

}

void haffmancode(haffnode hafftree[],int n,code haffcode[])

{

code cd; int i,j; int child,parent;

for( i=0;i< n;i++)

{

cd.start=n-1;

cd.weight=hafftree[i].weight;

cd.ch=hafftree[i].ch;

child=i;

parent=hafftree[child].parent;

while(parent!=0)

{

if(hafftree[parent].leftchild==child)

cd.bit[cd.start]=0;

else cd.bit[cd.start]=1;

cd.start--;

child=parent;

parent=hafftree[child].parent;

}

for(j=cd.start+1;j< n;j++)

haffcode[i].bit[j]=cd.bit[j];

haffcode[i].start=cd.start;

haffcode[i].weight=cd.weight;

haffcode[i].ch=cd.ch;

}

}
#include"string.h"
void ccode(haffnode hafftree[],int n)

{ 
	char str[100];
	cin>>str;
	int treenode=n*2-2;
	int len=strlen(str);
	for(int i=0;i<len;i++)
	{
		if(str[i]=='0')
		{
			treenode=hafftree[treenode].leftchild;
		}
		if(str[i]=='1')
		{
			treenode=hafftree[treenode].rightchild;
		}
		if(hafftree[treenode].leftchild==-1||hafftree[treenode].rightchild==-1)
		{
			cout<<hafftree[treenode].ch;
			treenode=n*2-2;
		}
	}
}

int main( )

{

int n=8;

int weight[]={5,29,7,8,14,23,3,11};

char text[]={'a','b','c','d','e','f','g','h'};

haffnode myhafftree[maxvalue];

code myhaffcode[maxvalue];

haffman(weight,text,n,myhafftree);

haffmancode(myhafftree,n,myhaffcode);

ccode(myhafftree,n);

return 0;

}

1105: 交换二叉树的孩子结点

#include"iostream"
using namespace std;
char s[100];
typedef struct tree
{
	struct tree* left,*right;
	char data;
}tree;
tree* buynode(char ch)
{
	tree* root=(tree*)malloc(sizeof(tree));
	root->data=ch;
	root->left=root->right=NULL;
	return root;
}
tree* create(char s[],int*data)
{
	if(s[*data]=='#'||s[*data]=='\0')
	{
		(*data)++;
		return NULL;
	}
	tree* root=buynode(s[*data]);
	(*data)++;
	root->left=create(s,data);
	root->right=create(s,data);
	return root;
}
void order(tree* root)
{
	if(root!=NULL)
	{
		order(root->right);
		cout<<root->data;
		order(root->left);
	}
}
void prder(tree* root)
{
	if(root!=NULL)
	{
		cout<<root->data;
		prder(root->right);
		prder(root->left);
	}
}
int main()
{
	cin>>s;
	int data=0;
	tree* root=create(s,&data);
	order(root);
	cout<<endl;
	prder(root);
}

1077: 平衡二叉树的判定

#include"iostream"
using namespace std;
char s[100];
#include"cstring"
typedef struct tree
{
	struct tree* left,*right;
	char data;
}tree;
tree* buynode(char ch)
{
	tree* root=(tree*)malloc(sizeof(tree));
	root->data=ch;
	root->left=root->right=NULL;
	return root;
}
tree* create(char s[],int* data)
{
	if(s[*data]=='#'||s[*data]=='\0')
	{
		(*data)++;
		return NULL;
	}
	tree* root=buynode(s[*data]);
	(*data)++;
	root->left=create(s,data);
	root->right=create(s,data);
	return root;
}
int depth(tree* root)
{
	if(root==NULL) return 0;
	int left=depth(root->left);
	int right=depth(root->right);
	return left>right?left+1:right+1;
}
bool balance(tree* root)
{
	if(root==NULL) return 1;
	if(root->left==NULL&&root->right==NULL) return 1;
	
	int left=depth(root->left);
	int right=depth(root->right);
	return abs(left-right)<=1&&balance(root->left)
	&&balance(root->right);
	
}
int main()
{
	cin>>s;
	int data=0;
	tree* root=create(s,&data);
	if(balance(root)==1) cout<<"yes!";
	else cout<<"no!";
}

1014: 交换排序算法的设计与实现——冒泡排序

#include"iostream"
using namespace std;
int a[100];
int n;
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i;j<n-i-1;j++)
		{
			if(a[j]>a[j+1])
			{
				int tmp=a[j+1];
				a[j+1]=a[j];
				a[j]=tmp;
			}
		}
		break;
	}
	for(int i=0;i<n;i++)
	{
		cout<<a[i]<<" ";
	}
}

1053: 输出利用先序遍历创建的二叉树中的指定结点的度

#include"iostream"
using namespace std;
int a[100];
int n;
char s[100];
typedef struct tree
{
	struct tree* left,*right;
	char data;
}tree;
tree* buynode(char ch)
{
	tree* root=(tree*)malloc(sizeof(tree));
	root->data=ch;
	root->left=root->right=NULL;
	return root;
}
tree* create(char s[],int*data)
{
	if(s[*data]=='#'||s[*data]=='\0')
	{
		(*data)++;
		return NULL;
	}
	tree* root=buynode(s[*data]);
	(*data)++;
	root->left=create(s,data);
	root->right=create(s,data);
	return root;
}
int find(tree* root,char* key)
{
	if(root==NULL) return 0;
	if(root->data==*key)
	{
		if(root->left!=NULL&&root->right!=NULL) return 2;
		else if(root->left!=NULL||root->right!=NULL) return 1;
		else return 0;
	}
	
	int left=find(root->left,key);
	int right=find(root->right,key);
	if(left>0||right>0)
	return 1;

}
int main()
{
	cin>>s;
	char key;
	cin>>key;
	int data=0;
	tree* root=create(s,&data);
	cout<<find(root,&key);
}

1010: 折半查找的实现

#include"iostream"
using namespace std;
int a[100];
int main()
{
	int n=0;
	cin>>n;
	int data=0;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	int key=0;
	cin>>key;
	int left=0;
	int right=n-1;
	int cnt=0;
	while(left<=right)
	{
		int mid=(left+right)/2;
		if(a[mid]<key)
		{
			left=mid+1;
			cnt++;
		}
		if(a[mid]>key)
		{
			right=mid-1;
			cnt++;
		}
		if(a[mid]==key)
		{
			cout<<mid<<endl<<cnt;
			return 0;
		}
	}
	cout<<"-1"<<endl<<cnt;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2379407.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

机器学习 Day18 Support Vector Machine ——最优美的机器学习算法

1.问题导入&#xff1a; 2.SVM定义和一些最优化理论 2.1SVM中的定义 2.1.1 定义 SVM 定义&#xff1a;SVM&#xff08;Support Vector Machine&#xff0c;支持向量机&#xff09;核心是寻找超平面将样本分成两类且间隔最大 。它功能多样&#xff0c;可用于线性或非线性分类…

答题pk小程序道具卡的获取与应用

道具卡是答题PK小程序中必不可少的一项增加趣味性的辅助应用&#xff0c;那么道具卡是如何获取与应用的呢&#xff0c;接下来我们来揭晓答案&#xff1a; 一、道具卡的获取&#xff1a; 签到获取&#xff1a;在每日签到中签到不仅可获得当日的签到奖励积分&#xff0c;同时连…

leetcode3265. 统计近似相等数对 I-medium

1 题目&#xff1a;统计近似相等数对 I 官方标定难度&#xff1a;中 给你一个正整数数组 nums 。 如果我们执行以下操作 至多一次 可以让两个整数 x 和 y 相等&#xff0c;那么我们称这个数对是 近似相等 的&#xff1a; 选择 x 或者 y 之一&#xff0c;将这个数字中的两个…

【架构篇】代码组织结构设计

代码组织结构设计&#xff1a;模块化分层与高效协作实践 摘要 本文以Java项目为例&#xff0c;解析后端代码组织的标准化结构&#xff0c;涵盖模块划分原则、依赖管理策略及实际应用场景。通过模块化设计提升代码可维护性、团队协作效率及系统扩展能力。 一、模块化设计的核心…

日期数据渲染转换问题

今天在学习Springboot框架时&#xff0c;想做一个非常简单的增删改查巩固一下&#xff0c;结果在数据渲染上出现了一个小问题&#xff0c;如图数据库中的数据一切正常 但是在前端渲染时&#xff0c;是下面这个效果 这是因为数据库存储的日期类型数据在前端渲染时&#xff0c;没…

ubuntu18.04编译qt5.14.2源码

ubuntu18.04编译qt5.14.2源码 文章目录 ubuntu18.04编译qt5.14.2源码[toc]1 前言2 参考文档3 下载源码3.1 方法13.2 方法23.3 方法3 4 ubuntu编译qt源码4.1 环境准备4.2 设置交换分区大小4.3 编译源码4.4 添加环境变量4.5 验证编译结果4.6 编译帮助文档&#xff08;qch&#xf…

创建指定版本的vite项目

1、获取vite的版本号 npm view create-vite versions 注:4.4.1版本即对应着node16版本的项目 2、创建制定版本的vite项目 npm init vite<version>

iOS 初识RunLoop

iOS 初识RunLoop 文章目录 iOS 初识RunLoopRunLoop的概念RunLoop的功能RunLoop和线程的关系RunLoop的结构ModeObserverTimer 和 source小结 RunLoop的核心RunLoop的流程RunLoop的应用AutoreleasePool响应触控事件刷新界面常驻线程网络请求NSTimer 和 CADisplayLinkNSTimerGCDTi…

电子电路仿真实验教学平台重磅上线!——深圳航天科技创新研究院倾力打造,助力高校教学数字化转型

在传统电子电路课堂中&#xff0c;实验室的灯光总与高昂的成本、拥挤的设备、反复的耗材损耗相伴&#xff0c;而教师不得不面对这样的现实&#xff1a;有限的硬件资源束缚着教学深度&#xff0c;不可逆的实验风险制约着创新探索&#xff0c;固化的时空场景阻碍着个性化学习。当…

搭建一个WordPress网站需要多少成本

WordPress 最初可能只是一个简单的博客平台。但近年来&#xff0c;它不仅成为了最好的博客平台&#xff0c;还成为了一个全面的内容管理系统。白宫、jQuery、NGINX、《纽约时报》等企业都把 WordPress 作为自己的网上家园。 不过&#xff0c;它们只是其中的佼佼者。根据 Built…

Python数据可视化 - Pyecharts绘图示例

文章目录 一、Pyecharts简介及安装1. Pyecharts简介2. 安装Pyecharts 二、准备数据三、饼图示例1. 初始化选项配置2. 饼图相关设置3. 全局配置项3.1 标题配置项3.2 图例配置项3.3 提示框配置项3.4 工具箱配置项3.5 视觉映射配置项 4. 系列配置项4.1 标签选项配置4.2 图元样式配…

NC016NC017美光固态芯片NC101NC102

NC016NC017美光固态芯片NC101NC102 在存储技术的演进历程中&#xff0c;美光科技的NC016、NC017、NC101与NC102系列固态芯片&#xff0c;凭借其技术创新与市场适应性&#xff0c;成为行业关注的焦点。本文将从技术内核、产品性能、行业动向、应用场景及市场价值五个维度&#…

[Android] 青木扫描全能文档3.0,支持自动扫描功能

声明&#xff1a;根据许多帖友的反馈&#xff0c;我也根据重新实测得出结论&#xff1a;该app是提供一天的体验时间&#xff0c;后续还是采取收费才能使用功能的措施。因为现在市面上免费使用的扫描工具很少了&#xff0c;所以当初我初步测试感觉软件不错就发布了出来&#xff…

通俗解释Transformer在处理序列问题高效的原因(个人理解)

Transformer出现的背景 CNN 的全局关联缺陷卷积神经网络&#xff08;CNN&#xff09;通过多层堆叠扩大感受野&#xff0c;但在自然语言处理中存在本质局限&#xff1a; 局部操作的语义割裂&#xff1a;每个卷积核仅处理固定窗口&#xff08;如 3-5 词&#xff09;&#xff0c;…

区间带边权并查集,XY4060泄露的测试点

目录 一、题目 1、题目描述 2、输入输出 2.1输入 2.2输出 3、原题链接 二、解题报告 1、思路分析 2、复杂度 3、代码详解 一、题目 1、题目描述 2、输入输出 2.1输入 2.2输出 3、原题链接 码蹄集 二、解题报告 1、思路分析 关于带边权并查集&#xff1a;并查集&…

【数据结构】1-4算法的空间复杂度

数据结构知识点合集 知识点 空间复杂度的定义以及计算 空间复杂度--空间开销&#xff08;内存开销&#xff09;与问题规模 n 之间的关系 无论问题规模怎么变&#xff0c;算法运行所需的内存空间都是固定的常量&#xff0c;算法空间复杂度为S(n) O(1)&#xff0c;S 表示 “Spac…

OpenAI推出Codex — ChatGPT内置的软件工程Agents

OpenAI继续让ChatGPT对开发者更加实用。 几天前,他们增加了连接GitHub仓库的支持,可以"Deep Research"并根据你自己的代码提问。 今天,该公司在ChatGPT中推出了Codex的研究预览版,这是迄今为止最强大的AI编码Agent。 它可以编写代码、修复错误、运行测试,并在…

AI日报 · 2025年5月15日|GPT-4.1 登陆 ChatGPT

AI日报 2025年5月15日&#xff5c;GPT-4.1 登陆 ChatGPT 1、OpenAI 在 ChatGPT 全面开放 GPT-4.1 与 GPT-4.1 mini 北京时间 5 月 14 日晚&#xff0c;OpenAI 在官方 Release Notes 中宣布&#xff1a;专为复杂代码与精细指令场景打造的 GPT-4.1 正式加入 ChatGPT&#xff0…

W5500使用ioLibrary库创建TCP客户端

1、WIZnet全硬件TCP/IP协议栈 WIZnet全硬件TCP/IP协议栈,支持TCP,UDP,IPv4,ICMP,ARP,IGMP以及PPPoE协议。 以太网&#xff1a;支持BSD和WIZCHIP&#xff08;W5500/W5300/W5200/W5100/W5100S&#xff09;的SOCKET APIs驱动程序。 互联网&#xff1a; DHCP客户端 DNS客户端 FTP客…

组态王|如何创建组态王工程?

哈喽,你好啊,我是雷工! 组态王是比较普及的组态软件之一,大部分工控人应该都接触过组态王软件, 最近有个用组态王软件开发上位机,对设备进行集中控制的项目,边开发,顺便记录一些使用方法。 本篇从基础的如何创建组态王工程开始记录,以下为操作笔记。 1 、首先在工程…