24考研王道408数据结构-第三章“栈、队列、数组”-p96课后算法题

第一题:

在这里插入图片描述

#include<iostream>
#include<stack>
using namespace std;
bool solution(string s){
	stack<char> st;
	int len=s.length();
	char temp;
	for(int i=0;i<len;i++){
		if(s[i]==')'||s[i]=='}'||s[i]==']'){
			if(st.empty()){//遇到右括号先判断栈是否为空 
				return false;
			}	
			else{
				temp=st.top();
				st.pop();
				if(s[i]==')'&&temp=='('){
					continue;
				}else if(s[i]=='}'&&temp=='{'){
					continue;
				}else if(s[i]==']'&&temp=='['){
					continue;
				}else{
					return false;
				}
			}
		}else{
			st.push(s[i]);
		}
	}
	if(st.empty()){
		return true;
	}else{
		return false;
	}
} 

int main(){
	string s="([{}])";
	bool flag=solution(s);
	if(flag){
		cout<<"true";
	}else{
		cout<<"false";
	}
}

在这里插入图片描述

第二题

在这里插入图片描述
算法思想:
在这里插入图片描述

#include<iostream>
#include<stack>
#include<vector>
#define maxSize 5
using namespace std;
char* solution(char str[]){
	stack<char>st;
	char train[maxSize];
	int j=0;
	for(int i=0;i<maxSize;i++){
		if(str[i]=='H'){
			st.push(str[i]);
		}else{
			train[j++]=str[i];
		}
	}
	char temp;
	while(!st.empty()){
		temp=st.top();
		st.pop();
		train[j++]=temp;
	}
	return train;
}
int main(){
	char tr[]="SHSSH";
	char *sortArr=solution(tr);
	for(int i;i<maxSize;i++){
		cout<<sortArr[i];
	}
}

在这里插入图片描述

第三题:

在这里插入图片描述
在这里插入图片描述

#include<iostream>
using namespace std;
#define maxSize 10
struct stack{
	int n;
	double pnx;
}st[maxSize];//注意需要自定义栈的结构体 

double ans(int n,double x){
	double fv1=1,fv2=2*x;
	int top=-1;
	for(int i=n;i>=2;i--){//越往上n的值越小 
		top++;
		st[top].n=i;
	}
	
	while(top>=0){
		st[top].pnx=2*x*fv2-2*(st[top].n-1)*fv1;
		fv1=fv2;
		fv2=st[top].pnx;
		top--;
	}
	if(n==0){
		return fv1;
	}
	return fv2;
}

int main(){
	double ans1=ans(2,1.0);
	cout<<ans1<<endl;
	return 0;
}

在这里插入图片描述

第四题:

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<queue>
using namespace std;
void disp(queue<char>&s){
	char c;
	while(!s.empty()){
		c=s.front();
		s.pop();
		cout<<c<<" ";
	}
	cout<<endl;
} 

void ans(){
	queue<char> q;
	queue<char> q1;//客车队列 
	queue<char> q2;//货车队列 
	
	for(int i=0;i<9;i++){
		q1.push('k');
	}
	for(int j=0;j<6;j++){
		q2.push('h');
	}
	int i=0,j=0;//j表示渡船上的总车辆数 
	char temp;
	while(j<10){
		if(!q1.empty()&&i<4){//客车队列不空且未上足四辆 
			temp=q1.front();
			q1.pop();
			q.push(temp);
			i++;//渡船上的客车数+1 
			j++;
		}
		else if(i==4&&!q2.empty()){//客车已经上足四辆 
			temp=q2.front();
			q2.pop();
			q.push(temp);
			j++;
			i=0; 
		}else{//其他情况(客车队列空或货车队列空) 
			while(j<10&&i<4&&!q2.empty()){
				temp=q2.front();
				q2.pop();
				q.push(temp);
				i++; 
			}
			i=0; 
		} 
		if(q1.empty()&&q2.empty()){//货车和客车加起来不足10辆 
			j=11;
		}
	}
	cout<<"渡船队列为"<<endl;
	disp(q);
}

int main(){
	ans();
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值