队列 程序 C++
2024-03-09 12:01:54
发布于:广东
#include<bits/stdc++.h>
using namespace std;
int q[n+1]; //自行更改
int head = 0;
int tail = 0;
void push(int t)
{
	++tail;
	q[tail] = t;
}
void pop()
{
	++head;
}
int front()
{
	return q[head+1];
}
int back()
{
	return q[tail];
}
int size()
{
	return tail - head;
}
bool empty()
{
	return tail - head == 0;
}
int main()
{
	
}
咱们来看一道例题:
小码王智能学习系统:C++ L1 - 第38课:解密QQ
传送门:小码王智能学习系统
#include<bits/stdc++.h>
using namespace std;
char q[10001];
int head = 0;
int tail = 0;
void push(int t)
{
	++tail;
	q[tail] = t;
}
void pop()
{
	++head;
}
char front()
{
	return q[head+1];
}
int back()
{
	return q[tail];
}
int size()
{
	return tail - head;
}
bool empty()
{
	return tail - head == 0;
}
int main()
{
	string s;
 	cin>>s;
 	for(int i=0;i<s.size();i++) push(s[i]);
 	while(!empty())
 	{
 		cout<<front();
 		pop();
 		push(front());
 		pop();
	}
	return 0;
}
咱们来看第二道例题:
小码王智能学习系统:C++ L1 - 第38课:解密QQ - 小码过河:鱼的记忆
#include<bits/stdc++.h>
using namespace std;
char q[10001],a[110];
int head = 0;
int tail = 0;
bool check(int l,int r,int x)
{
	for(int i=0;i<r-l;i++)
	{
		if(q[i] == x) return false;
	}
	return true;
}
int main()
{
	int n,m;
	int cnt = 0;
	cin>>n>>m;
	char t;
	for(int i=1;i<=n;i++) 
	{
		cin>>a[i];
	}
	for(int i=1;i<=n;i++) 
	{
		if(check(head,tail,a[i]))
		{
			if(tail - head == m) ++head;
			q[++tail] = a[i];
			cnt++;
		}
		
	}
	cout<<cnt;
	return 0;
}
全部评论 1
顶
2024-03-09 来自 广东
0








有帮助,赞一个