数据结构与算法--第一章题解

选择

数据结构研究的内容:D数据的逻辑结构、存储结构及其基本操作

算法分析的两个主要方面是:A. 空间复杂度和时间复杂度

时间:B. O(n2)

空间:A. O(1)

第一节题外题:求解约瑟夫问题
#include <malloc.h>
#include <iostream>
using namespace std;

typedef struct node
{
	int no;			//小孩编号
	struct node *next;		//指向下一个结点指针
} Child;

void CreateList(Child *&L, int n) //建立有n个结点的循环单链表
{
	int i;  Child *p, *tc;	//tc指向新建循环单链表的尾结点
	L = (Child *)malloc(sizeof(Child));
	L->no = 1;			//先建立只有一个no为1结点的单链表
	tc = L;
	for (i = 2; i <= n; i++)
	{
		p = (Child *)malloc(sizeof(Child));
		p->no = i;		//建立一个存放编号i的结点
		tc->next = p; tc = p;	//将p结点链到末尾
	}
	tc->next = L;			//构成一个首结点为L的循环单链表
}
void Joseph(int n)  //求解约瑟夫序列
{
	int i, j;  Child *L, *p, *q;
	CreateList(L, n);
	int m = 5;
	//输入代码
	int l = 0;
    for(int i = 1;i <= n;i++)
	{
        l = (l + m) % i;
    }
    printf("%d\n", l + 1);
    //输入代码
}
int main()
{
	int n;
	cin>> n;
	Joseph(n);
	system("pause");
	return 0;
}
第一节题外题:括号匹配
#include<iostream>
#include<string.h>
using namespace std;

#define MAX_SIZE 100

typedef char ElemType;
typedef struct {	
	ElemType data[MAX_SIZE];
	int top;
} SqStack;

void InitStack(SqStack &st)
{
	st.top=-1;
}

int PushStack(SqStack &st, ElemType x)
{	if (st.top == MAX_SIZE-1)	
		return 0;
	else {	
		st.top++;
		st.data[st.top] = x;
		return 1;
	}
}

int PopStack(SqStack &st, ElemType &x)
{	
	if (st.top == -1)
		return 0;
	else 
	{	
		x = st.data[st.top];
		st.top--;
		return 1;
	}
}

int isEmpty(SqStack st)
{	
	if (st.top == -1) 
		return 1;
	else 
		return 0;
}

char GetTop(SqStack st,ElemType &x)
{
	if(st.top==-1)
		return 0;
	else
	{
		x=st.data[st.top];
		return x;
	}
}

int main()
{
	SqStack st;
	InitStack(st);
	char opstr[MAX_SIZE];
	cin >> opstr;
	int len;ElemType t,e;
	len = strlen(opstr);
	for(int i=0;i<len;i++)
	{
		if(opstr[i]=='{'||opstr[i]=='('||opstr[i]=='[')
			PushStack(st,opstr[i]);
		else if(opstr[i]==')')
		{
			if(isEmpty(st))
			{
				cout<<"Extra right brackets"<<endl;
				return 0;
			}
			GetTop(st,t);
			if(t=='(')
				PopStack(st,e);
			else
			{
				cout<<"Brackets not match"<<endl;
                return 0;
			}
		}
		else if(opstr[i]==']')
		{
			if(isEmpty(st))
			{
				cout<<"Extra right brackets"<<endl;
				return 0;
			}
			GetTop(st,t);
			if(t=='[')
				PopStack(st,e);
			else
			{
				cout<<"Brackets not match"<<endl;
                return 0;
			}
		}
		else if(opstr[i]=='}')
		{
			if(isEmpty(st))
			{
				cout<<"Extra right brackets"<<endl;
				return 0;
			}
			GetTop(st,t);
			if(t=='{')
				PopStack(st,e);
			else
			{
				cout<<"Brackets not match"<<endl;
                return 0;
			}
		}
	}
	if(isEmpty(st))//栈空-匹配成功 
		cout<<"Brackets match"<<endl;
	else
	{
		GetTop(st,t);
		if(t=='{'||t=='['||t=='(')
			cout<<"Extra left brackets"<<endl;
	}
	return 0;
}
05 递归求阶乘
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;

int recursion(int n){
	if(n==1||n==0) return 1;
	else return n*recursion(n-1);
}

int main(){
	int n;
	cin>>n;
	cout<<recursion(n)<<endl;
	return 0; 
}

06角谷定理-递归
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int ans = 0;

void recursion(int n) {
	if (n == 1) return;
	if (n % 2 == 0) {
		ans++;
		n /= 2;
		recursion(n);
	}
	else {
		ans++;
		n = n * 3 + 1;
		recursion(n);
	}
}

int main() {
	int n;
	cin >> n;
	recursion(n);
	cout << ans << endl;
	return 0;
}

07输出数组中大于等于平均数的数
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;


int main(){
	int n;
	cin>>n;
	int a[n];
	int sum=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	double av=(sum*1.0)/n;
	
	int flag=1;
	for(int i=0;i<n;i++){
		if(a[i]>=av){
			if(flag==1) {
				cout<<a[i];
				flag=0;
			}
			else cout<<" "<<a[i];
		}
		
	} 
	return 0; 
}

08超级求和-前缀和
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#define maxn 1E7
typedef long long ll; 
using namespace std;


int main(){
	int n;
	cin>>n;
	int sum_a[n];
	sum_a[0]=0;
	for(int i=1;i<=n;i++){
		sum_a[i]=sum_a[i-1]+i; //前缀和 
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		ans+=sum_a[i];
	}
	
	cout<<ans<<endl;
	return 0; 
}

09.数位之和
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#define maxn 1E7
typedef long long ll; 
using namespace std;


int main(){
	int a,ans=0;
	cin>>a;
	while(a!=0){
		ans+=(a%10);
		a/=10;
	}
	cout<<ans<<endl;
	return 0; 
}

10爬楼梯-递归
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#define maxn 1E7
typedef long long ll; 
using namespace std;

int solve(int n){
	if(n==1)return 1;
	else if(n==2) return 2;
	else return solve(n-1)+solve(n-2);
}


int main(){
	int k,n;
	cin>>k;
	while(k--){
		cin>>n;
		cout<<solve(n)<<endl;
	}
	
	return 0; 
}

11递归求甲的年龄
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#define maxn 1E7
typedef long long ll; 
using namespace std;

int solve(int n){
	if(n==8)return 4;
	else return solve(++n)*2-3; 
}


int main(){
	int n=1;
	cout<<solve(n)<<endl; 
	return 0; 
}

12递归求最大公约数

辗转相除法

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#define maxn 1E7
typedef long long ll; 
using namespace std;

int gcd(int a,int b){
	int x=min(a,b);
	int y=max(a,b);
	if(x==0) return y;
	else return gcd(x,y%x);
}


int main(){
	int a,b;
	cin>>a>>b;
	cout<<gcd(a,b)<<endl; 
	return 0; 
}

13递归 逆向输出字符串
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#define maxn 1E7
typedef long long ll;
using namespace std;

char s[20];

void convert(int n, int N)//n为元素的下标,N为数组的实际长度 
{
    if (n < N) {
        convert(n + 1, N);
        printf("%c", s[n]);
    }
}

int main()
{
    scanf("%s", s);
    convert(0, strlen(s));
    printf("\n");
    return 0;

}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值