20231110_stack_queue

发布时间 2023-11-10 19:37:15作者: HelloHeBin

课程笔记

https://www.cnblogs.com/hellohebin/p/15677386.html

上课代码

// 1-10
/* // test1
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int sta[N], head=0;
int n, x;

int main(){
    cin>>n;
    for(int i=1; i<=n; i++){
        cin>> x;
        sta[++ head] = x; // 入栈 [1, head]
    }
    
    while(head){
        cout<<sta[head] <<" ";
        --head; 
    } 
    return 0;
}
*/ 

/* // test2
//P1739 表达式括号匹配
//表达式有英文字母(小写)、运算符( + 、 - 、 * 、 / )和左右小(圆)括号构成,以 @ 作为表达式的
//结束符。请编写一个程序检查表达式中的左右圆括号是否匹配;
//若匹配,则输出 YES ;
//否则输出 NO 。
//表达式长度小于 ,左圆括号少于 个。
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
char sta[N];
int head;

int main(){
    string s; cin>>s;
    for(int i=0; i<s.size(); i++){
        if(s[i]=='(') sta[++head] = '(';
        if(s[i]==')'){
            if(head) head--;
            else {
                head = -1;
                break;
            } 
        }
    }
    if(head == 0 ) cout<<"YES";
    else  cout<<"NO";
}

int main1(){
    string s; cin>>s;
    int h=0;
    for(int i=0; i<s.size(); i++){
        if( s[i]=='(' ) h ++;
        if( s[i]==')' ) h--;
        if( h < 0 ) break;
    }
    if(h == 0) cout<<"YES";
    else cout<<"NO";

    return 0;
}
*/

/* // test4
//P1996 约瑟夫问题
//n个人围成一圈,从第一个人开始报数,数到 的人出列,再由下一个人重新从 开始报数,数到 的
//人再出圈,依次类推,直到所有的人都出圈,请输出依次出圈人的编号。
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;

int que[N], front = 0, tail=-1;

int main(){
    int n, m, cnt=0; cin>>n>>m;
    for(int i=1; i<=n; i++)  que[++tail] = i;
    while(front <= tail){
        int u = que[front];
        front++;
        cnt ++;
        if(cnt % m ==0 ) cout<< u<<" ";
        else que[++tail]  = u;
    }
    return 0;
}
*/