大一集训--c++队列queue

队列queue

题目一:P1634

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m;
    while(cin >> n >> m)
    {
        queue<int>people;
        for(int i=1;i<=n;i++)
        {
            people.push(i);
        }
        while(people.size()!=1)
        {
            for(int i=1;i<=m;i++)
            {
                if(i==m)
                {
                    people.pop();
                    break;
                }
                people.push(people.front());
                people.pop();
            }
        }
        cout << people.front() << endl ;
    }
    return 0;
}

题目二:P1633

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,k,p;
    while(cin >> n >> k >> p)
    {
        queue<int>pai;
        for(int i=1; i<=k; i++)
        {
            pai.push(i);
        }
        int a[50005],flag=0;
        for(int i=1; i<=k; i++)
        {
            if(i%n==0)
                a[++flag]=pai.front();
            pai.pop();
            for(int j=1; j<=p; j++)
            {
                pai.push(pai.front());
                pai.pop();
            }
        }
        sort(a+1,a+1+flag);
        for(int i=1; i<=flag; i++)
        {
            cout << a[i] << endl ;
        }
    }
    return 0;
}

题目三:P1635

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m,x;
    while(cin >> n >> m >> x)
    {
        queue<string>name;
        for(int i=1;i<=n;i++)
        {
            string s;
            cin >> s;
            name.push(s);
        }
        while(--m)
        {
            name.push(name.front());
            name.pop();
        }
        for(int i=x;i;i++)
        {
            if(name.size()==1)
                break;
            int t=i;
            if(!(t%7))
            {
                name.pop();
                continue;
            }
            while(t)
            {
                if(t%10==7)
                {
                    name.pop();
                    goto loop;
                }
                t/=10;
            }
            name.push(name.front());
            name.pop();
            loop:continue;
        }
        cout << name.front() << endl ;
    }
    return 0;
}

题目四:P1636

#include <bits/stdc++.h>
using namespace std;
struct _people
{
    int t;
    int x;
}people[300005];
int a[300005];
int main()
{
    int n;
    int ans=0;
    cin >> n;
    queue<_people>que;
    for(int i=1;i<=n;i++)
    {
        int t,k;
        cin >> t >> k;
        for(int j=1;j<=k;j++)
        {
            people[j].t=t;
            cin >> people[j].x;
            if(!a[people[j].x])
                ans++;
            a[people[j].x]++;
            if(j==1&&!que.empty())
            {
                while(!que.empty()&&t-que.front().t>=86400)
                {
                    a[que.front().x]--;
                    if(!a[que.front().x])
                        ans--;
                    que.pop();
                }
            }
            que.push(people[j]);
        }
        cout << ans << endl ;
    }
    return 0;
}

题目五:P1663

#include <bits/stdc++.h>

using namespace std;
int main()
{
    int n,x,y;
    int pass=0;
    int a[105][105];
    int p[105],p2[105];
    while(cin >> n >> x >> y)
    {
        memset(p,0,sizeof(p));
        memset(p2,0,sizeof(p2));
        pass=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                cin >> a[i][j];
            }
        }
        p[x]++;
        while(1)
        {
            for(int i=1; i<=n; i++)
            {
                if(p[i])
                {
                    for(int j=1; j<=n; j++)
                    {
                        if(a[i][j])
                            p2[j]=1;
                    }
                }
            }
            if(!p2[y])
            {
                pass++;
                for(int i=1;i<=n;i++)
                {
                    p[i]=p2[i];
                }
            }
            else
            {
                break;
            }
        }
        cout << pass << endl ;
    }
    return 0;
}

题目六:P1662

#include <bits/stdc++.h>

using namespace std;
int x[100005];
int main()
{
    int a,n;
    while(cin >> a >> n)
    {
        memset(x,0,sizeof(x));
        x[1]=a;
        int two,three;
        two=three=1;
        for(int i=2;i<=n;i++)
        {
            x[i]=min(x[two]*2+1,x[three]*3+1);
            if(x[i]==x[two]*2+1)
                two++;
            if(x[i]==x[three]*3+1)
                three++;
        }
        cout << x[n] << endl ;
    }
    return 0;
}

题目七:P1632

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m,k;
    while(cin >> n >> m >> k)
    {
        queue<int>man;
        queue<int>woman;
        for(int i=1;i<=n;i++)
        {
            man.push(i);
        }
        for(int i=1;i<=m;i++)
        {
            woman.push(i);
        }
        for(int i=1;i<=k;i++)
        {
            cout << man.front() << " " << woman.front() << endl ;
            man.push(man.front());
            man.pop();
            woman.push(woman.front());
            woman.pop();
        }
    }
    return 0;
}
发布了44 篇原创文章 · 获赞 13 · 访问量 2343

猜你喜欢

转载自blog.csdn.net/NEFU_kadia/article/details/104278909