114514
2024-01-18 10:11:04
发布于:广东
双端队列应用
#include<bits/stdc++.h>
using namespace std;
//创建结构体
struct Student{
//封装属性
string name;
string hobby;
int age;
//封装方法
void intro(){
cout<<"名字:"<<name<<";爱好"<<hobby<<";年龄:"<<age<<endl;
}
};
deque<Student> dq; //双端队列只能存储Student
int main(){
Student s1={"1","打篮球",12};
Student s2={"2","打篮球",12};
Student s3={"3","打篮球",12};
Student s4={"4","打篮球",12};
Student s5={"5","打篮球",12};
Student s6={"6","打篮球",12};
Student s7={"7","打篮球",12};
//前四个人从队尾入队
dq.push_back(s1);
dq.push_back(s2);
dq.push_back(s3);
dq.push_back(s4);
//后三个人从队头入队
dq.push_front(s5);
dq.push_front(s6);
dq.push_front(s7);
//从队首依次出队
while(dq.size()>0){
Student t=dq.front();
t.intro();
dq.pop_front();
}
return 0;
}
礼品摆放
#include<bits/stdc++.h>
using namespace std;
queue<int> q;
int m,n,x,ans=0;
bool vis[10001];
int main(){
cin>>m>>n;
for(int i=1;i<=n;i++){
cin>>x;
if(vis[x]==1) continue;
if(q.size()==m){
int f=q.front();
q.pop();
vis[f]=0;
}
q.push(x);
ans++;
vis[x]=1;
}
cout<<ans;
return 0;
}
7前缀和
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,ans;
cin>>n;
int a[100001],pre[100001],r[7],l[7]={0,-1,-1,-1,-1,-1,-1};
for(int i=1;i<=n;i++){
cin>>a[i];
pre[i]=(pre[i-1]+a[i])%7;
if(l[pre[i]]==-1) l[pre[i]]=i;
r[pre[i]]=i;
}
for(int i=0;i<=6;i++) ans=max(ans,r[i]-l[i]);
cout<<ans;
return 0;
}
围圈报数
#include<bits/stdc++.h>
using namespace std;
queue<int> q;
int n,m;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
q.push(i);
}
while(q.size()!=1){
for(int i=1;i<m;i++){
int t=q.front();
q.pop();
q.push(t);
}
cout<<q.front()<<" ";
q.pop();
}
cout<<q.front();
return 0;
}
表达式括号匹配
#include<bits/stdc++.h>
using namespace std;
char s[1005],top=0;
void push(char x){
top++;
s[top]=x;
}
void pop(){--top;}
char ***(){return s[top];}
bool empty(){return top==0;}
void clear(){top=0;}
int main(){
string a;
cin>>a;
for(int i=0;i<a.length();i++){
if(a[i]=='(') push(a[i]);
if(a[i]==')'){
if(!empty()) pop();
else{
cout<<"NO";
return 0;
}
}
}
if(empty()) cout<<"YES";
else cout<<"NO";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
char a[110];
int top=0;
string str;
void push(int x){a[++top]=x;}
void pop(){--top;}
char gt(){return a[top];}
bool ie(){return top==0;}
int dop(){return a[top];}
int main(){
int n;
cin>>n;
while(n--){
string flag;
cin>>flag;
if(flag=="push"){
int k;
cin>>k;
a[++top]=k;
}
else if(flag=="pop"){
if(top!=0) {
cout<<"pop "<<dop()<<endl;
top--;
}
else cout<<"pop fail"<<endl;
}
else if(flag=="top"){
if(top!=0) cout<<"top = "<<dop()<<endl;
else cout<<"top fail"<<endl;
}
else if(flag=="size") cout<<"size = "<<top<<endl;
else if(flag=="empty"){
if(top!=0) cout<<"no"<<endl;
else cout<<"yes"<<endl;
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int a[N],top=0;
void push(char x){
a[++top]=x;
}
void pop(){
--top;
}
char gt(){
return a[top];
}
bool ie(){
return top==0;
}
int size(){
return top-0;
}
void clear(){
top=0;
}
int main(){
push('H');
push('e');
push('l');
push('N');
pop();
push('x');
cout<<"size = "<<size()<<endl;
cout<<"eletop = "<<gt()<<endl;
while(!ie()){
cout<<"栈顶元素:"<<gt()<<endl;
pop();
}
return 0;
}
这里空空如也
有帮助,赞一个