PTA--①银行业务队列简单模拟②全排列③最大子列和④出栈序列的合法性(有的没写·注释,看看代码就理解了)

①银行业务队列简单模拟,题目:

代码,及注释。 

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int a[N],b[N]; //存放到A,B 两个窗口 处理的人的编号 

int main()
{
	int n;cin>>n;
	int f = 0;//控制输出的空格
	int k = 0,j = 0;
	while(n--)
	{
		int idx;cin>>idx;
		if(idx%2) a[k++] = idx;
		else b[j++] = idx;
	} 
	int aidx = 0,bidx = 0;
	while(aidx < k || bidx < j)
	{
		if(aidx < k)
		{
			if(f) cout<<" "<<a[aidx];
			else cout<<a[aidx],f = 1;
			aidx ++;
			//当B 队列还有人 并且 已经 输出了 偶数个 顾客,轮到B队列输出了。 
			if(bidx < j && aidx % 2 == 0)
			{
				if(f) cout<<" "<<b[bidx];
				else cout<<b[bidx] ,f = 1;
				bidx ++;
			}
		}
		else if(bidx < j)
		{
				if(f) cout<<" "<<b[bidx];
				else cout<<b[bidx] ,f = 1;
				bidx ++;
		}
	} 
	return 0;
}

②全排列,题目:

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 10;
int p[N],n;
bool st[N];
void solve(int index)
{
	if(index == n + 1)
	{
		for(int i = 1; i <= n;i++)
		{
			cout<<p[i];
		}
		cout<<endl;
	}
	for(int x = 1;x <= n;x++)
	{
		if(st[x] == 0)
		{
			p[index] = x;
			st[x] = 1;
			solve(index+1);
			st[x] = 0;
		}
	}
}
int main()
{
	cin>>n;
	solve(1);
	return 0;
} 

 ③最大子列和问题,题目:

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;cin>>n;
	int sum = 0,mx = 0;
	while(n--)
	{
		int a;
		cin>>a;
		sum += a;
		if(sum < 0) sum = 0;
		mx = max(sum,mx);
	}
	cout<<mx<<endl;
	return 0;
}

④出栈序列的合法性 ,题目:

 

 代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,k;cin>>m>>n>>k;
	int out[n+10];
	while(k--)
	{
		int in = 1;
		stack<int> s;
		bool is_true = false;
		for(int i = 0; i < n;i++) cin>>out[i];
		for(int i = 0;i < n;i++)
		{
			if(!s.empty() && s.top() == out[i]) 
			     s.pop();
			else 
			{
				for(;in <= out[i] ;in++)
				{
					s.push(in);
					if(s.size() > m)
					{
						is_true = true;break;
					}
				}
				if(is_true) break;
				if(s.top() == out[i]) s.pop();
			}
		}
		if(s.empty() && !is_true) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
 	}
 	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值