从零起步看算法(第二十天 5.11)
又多加了几门课程的学习,任务多,负担大,就需要提高效率。
回归规律的学习生活,多到图书馆,拒绝宿舍。
//队列
1.基础操作
//基础操作
/*
#include <iostream>
#include<queue>
using namespace std;
int main() {
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
while(!q.empty()){
cout<<q.front()<<endl;
q.pop();
}
return 0;
}
*/
2.经典例题 报数
激烈经典方法
学会用队列的基本操作实现循环队列
//报数
/**
#include<iostream>
#include<string>
#include<queue>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
queue<int > q;
int count=0;
//经典套路
//初始化
for(int i=1;i<=n;i++){
q.push(i);
}
//剔除
while(q.size()!=1){
count++;
if(count==m){
q.pop();
count=0;
}
else{
q.push(q.front()); //形成循环
q.pop();
}
}
cout<<q.front()<<endl;
return 0;
}
*/
3.敲七
道理跟上题相通,注意题设细节。
#include<iostream>
#include<string>
#include<queue>
using namespace std;
int main(){
queue<int> q;
int n,m,t;
int count=0;
cin>>n>>m>>t;
string array[n+1];
for(int i=1;i<=n;i++){
cin>>array[i];
}
for(int i=1;i<=n;i++){
q.push(i);
}
count=t;
//找到真正的队首
for(int j=1;j<m;j++){
int temp=q.front();
q.push(temp);
q.pop();
}
if(count%7==0||count%10==7||count/10%10==7)
{
q.pop();
}
else{
q.push(q.front());
q.pop();
}
while(q.size()!=1){
count++;
//剔除
if(count%7==0){
q.pop();
continue;
}
if(count%10==7){
q.pop();
continue;
}
if(count/10%10==7){
q.pop();
continue;
}
if(count/100%10==7){
q.pop();
continue;
}
if(count/1000%10==7){
q.pop();
continue;
}
//形成循环
else{
int temp2=q.front();
q.push(temp2);
q.pop();
}
}
int tos=q.front();
cout<<array[tos]<<endl;
return 0;
}