首页 > 其他分享 >矿大数据结构 实验二

矿大数据结构 实验二

时间:2024-06-17 23:01:44浏览次数:15  
标签:cnt int s1 ++ 实验 矿大 MAXN 数据结构 root

 

目录

 

A: 子串个数

B.模式串

C.主对角线上的数据和

D.顺时针排螺旋阵

E: 汉诺塔游戏中的移动

F.树的先根遍历

G.树的后根遍历


A: 子串个数

本题未考虑重复的情况,直接使用公式既可

考虑重复的情况:不同子串个数 - 洛谷

#include <bits/stdc++.h>
using namespace std;
 
int cs(string s) {
    int n = s.length();
    return n * (n + 1) / 2+1;
}
string s;
int main() {
    while (getline(cin,s)){
        cout<<cs(s)<<'\n';
    }
    return 0;
}

B.模式串

应该是想考察KMP的,但由于样例很水,导致暴力就能拿满。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10000;
string tar,s;
int main(){
	cin>>s>>tar;
	for(int i=0;i<=s.length()-tar.length();i++){
		for(int j=0;j<tar.length();j++){
			if(s[i+j]!=tar[j]) break;
			if(j==tar.length()-1)
			{
				cout<<i+1;
				return 0;
			}
		}
	}
	cout<<0;
}

C.主对角线上的数据和

#include<bits/stdc++.h>
using namespace std;
long long sum = 0;
int main(){
	int n;cin>>n;
	int temp;
	cin>>temp;
	sum+=temp;	
	for(int i=1;i<n;i++){
		
		for(int i=1;i<=n;i++){
			cin>>temp;
		}
		cin>>temp;
		sum+=temp;
	}	
	cout<<sum;
}

D.顺时针排螺旋阵

模拟题,看成回型阵一层一层模拟即可

回是口中口

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 110;
int a[MAXN][MAXN];
int main(){
    int n,i=0,j,now=0;
    cin>>n;                 
    int r=1,cnt=n;            //r为层 
    while(r<=cnt){            //模拟,横五竖四 
        for( j=r   ;j<=cnt;j++) a[r][j]=++now;   //up 
        for(i=r+1  ;i<=cnt;i++) a[i][cnt]=++now;	 //right
        for(j=cnt-1;j>=r  ;j--) a[cnt][j]=++now;   //down
        for(i=cnt-1;i>r   ;i--) a[i][r]=++now;   //left
        r++;cnt--;
    }
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++)
           cout<<a[i][j]<<' ';
        printf("\n");
    }
}

E: 汉诺塔游戏中的移动

假设现在要把n个盘子移动到C上,那么我们需要做的就是将最大的那个盘子移动到C上,再将其余的盘子移动到C上。解决n-1个盘子的问题时亦是如此。而解决第n-1个盘子的问题时,刚刚所说的第 n 个盘子完全可以视而不见,故对结果没有影响。由此可见,不断递归下去,最终会来到1个盘子的问题,这样即可解决问题。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 110;
int cnt=0;
void solve(int n,char start,char mid,char tar){
	
	if(n==1){
		cnt++;
		printf("%d %d %c->%c\n",cnt,n,start,tar);
		return;
	}
	solve(n-1,start,tar,mid);
	cnt++;
	printf("%d %d %c->%c\n",cnt,n,start,tar);
	solve(n-1,mid,start,tar);
}
int main(){
    int n;cin>>n;
    solve(n,'A','B','C');

}

F.树的先根遍历

样例具有误导性,事实上这个题并不是二叉树。既然不是二叉树,那么使用结构体就略嫌麻烦。不如使用数组,用 t [i][j] 代表 i 的孩子分别是谁  cnt[i] 代表 i 有几个孩子

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10000;
int t[MAXN][30];
int cnt[MAXN];
void pre_order(int x){     //前序遍历
	cout<<(char)(x+'A')<<' ';
	for(int i=1;i<=cnt[x];i++){
		pre_order(t[x][i]);
	}
}
string s;
int main(){
	int root=0;    //用来确定谁是根
	int now = 0;
	char s1,s2;
	while(cin>>s1>>s2){
		if(now==0){
			root=s1-'A';
		}
		now++;      //将根初始化

		t[s1-'A'][++cnt[s1-'A']] = s2-'A';  //处理输入的数据

		if(s2-'A'==root)       //更新根
			root = s1-'A';
	}
	pre_order(root);
}

G.树的后根遍历

在上一题的基础上略加改动即可。先遍历孩子,再输出本身

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10000;
int t[MAXN][30];
int cnt[MAXN];
void aft_order(int x){
	
	for(int i=1;i<=cnt[x];i++){
		aft_order(t[x][i]);
	}
	cout<<(char)(x+'A')<<' ';
}
string s;
int main(){
	int root=0;
	int now = 0;
	char s1,s2;
	while(cin>>s1>>s2){
		if(now==0){
			root=s1-'A';
		}
		now++;
		
		t[s1-'A'][++cnt[s1-'A']] = s2-'A';
		
		if(s2-'A'==root)
			root = s1-'A';
	}
	aft_order(root);
}

标签:cnt,int,s1,++,实验,矿大,MAXN,数据结构,root
From: https://blog.csdn.net/2301_79879262/article/details/139654572

相关文章

  • C语言数据结构队列实现-链表队列
    简单实现了下链表队列代码如下#include<stdio.h>#include<stdlib.h>typedefstructNode{intdata;structNode*next;}Node;//入队列voidinsertList(Node*head,intelem){Node*temp=head;Node*newNode=(Node*)malloc(sizeof(Node));......
  • [C++][数据结构][红黑树]详细讲解
    目录1.红黑树的概念2.红黑树的性质3.红黑树节点的定义4.红黑树的结构5.红黑树的插入操作1.cur为红,p为红,g为黑,u存在且为红2.cur为红,p为红,g为黑,u不存在/u存在且为黑--单旋+变色3.cur为红,p为红,g为黑,u不存在/u存在且为黑--双旋+变色6.红黑树的迭代器1.begin()与end()2.o......
  • 实验7 文件应用编程
    task4.c1#include<stdio.h>23intmain(){4intcount=0;5charch;6FILE*fp;78fp=fopen("data4.txt","r");9if(fp==NULL){10printf("failtoopenfile\n");11......
  • (11.3)iic串口读写EEPROM实验:程序设计
    一、实验任务二、架构框图其中:i2c驱动模块: bit_ctrl:0代表发送8位字节地址;1代表发送16位字节地址(本实验采用)i2c_addr[15:0]:16位字节地址,当bit_ctrl为0时只有低8位是有效的i2c_data_w[7:0]:向EEPROM写入的8位数据i2c_exec:拉高代表当前进行......
  • 数据结构代码常用模板
    目录线性表顺序表单链表循环单链表栈和队列顺序栈链栈队列树与二叉树二叉树的遍历并查集哈夫曼树串KMP图深度优先搜索与广度优先搜索拓扑排序克洛斯卡尔最小生成树弗洛伊德最短路排序快速排序直接插入排序希尔排序简单选择排序冒泡排序线性表顺序表#include<iostream>#includ......
  • 实验7 文件应用编程
    task4.c1#include<stdio.h>2intmain()3{4FILE*fp;5charch;6intcount=0;7fp=fopen("data4.txt","r");8if(fp==NULL)9{10printf("failtoopenfile\n");11......
  • 实验7
    task4#include<stdio.h>intmain(){FILE*fp;intcount;count=0;charch;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfail\n");return1;}......
  • 实验7
    task1.c点击查看代码#include<stdio.h>#defineN80#defineM100typedefstruct{ charname[N];//书名 charauthor[N];//作者}Book;//函数声明voidfunc1();voidfunc2();intmain(){ func1();func2(); return0;}//函数func1定义......
  • 实验七
    task4.c#include<stdio.h>intmain(){intcount;charch;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");return1;}count=0;w......
  • SSH配置、跨主机上传下载、Wrapper访问控制实验操作步骤
    目录终端OpenSSH服务器SSH(SecureShell)协议OpenSSH服务监听选项SSH配置修改端口号用户登录控制指定用户登录1.2.严格模式最大会话数量公钥验证使用公钥认证让客户端登录系统域名解析跨主机下载、上传文件下载指定端口下载上传指定端口上传 ​编辑sftp功......