①银行业务队列简单模拟,题目:
代码,及注释。
#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;
}