循环链表,栈,队列

A - 小孩报数问题

 

#include <iostream>
using namespace std; 
char name[65][20]; // 存储孩子的名字
int main(int argc, const char * argv[]) {
    int n, w, s, i, count; // n存储孩子数, w:从第w个开始报数,s报到第s个
    char ch; // 把逗号去掉
    cin >> n; // 输入孩子数
    // 循环输入孩子的名字
    for(i = 0; i < n; i++)
        cin >> name[i];
    cin >> w >> ch >> s;
    count = 0;
    w = w - 2; // 减2是因为第一个比较特殊,下标从0开始减1,其次因为xiaohua没输出过,是第一个数
    while(count < n) { // 已经出列的人数
        for(i = 0; i < s; i++) {
            w = (w + 1) % n; // 求余,解决到尾要重新从0开始的问题,形成循环
            if(name[w][18] == '1') //如果此人已出列,不算
                i--;
        }
        cout << name[w] << endl;
        name[w][18] = '1'; //把此人标记为找过
        count++;
    }
    return 0;
}

 B - Tanning Salon

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char c;
    int n,flag,bed[26],aaa;
    while(1)
    {
            scanf("%d",&n);
        if(n==0)    exit(-1);
        flag=0;aaa=0;
        memset(bed,0,26*sizeof(int));
        getchar();
        while(scanf("%c",&c),c!='\n')
        {
        if(flag<n)
        if(bed[c-65]==-1)    ;
            else if(bed[c-65]==0)    {bed[c-65]=1;flag++;}
            else    {bed[c-65]=0;flag--;}
        else
            if(bed[c-65]==-1)    ;
            else if(bed[c-65]==1)    {bed[c-65]=0;flag--;}
            else    {bed[c-65]=-1;aaa++;}
        
        }
        if(aaa==0)    printf("All customers tanned successfully.\n");
        else    printf("%d customer(s) walked away.\n",aaa);
        
    }
}

C - Printer Queue

#include <iostream>

#include <cstdio>

#include <queue>
using namespace std;
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        queue<int> q;
        priority_queue<int> v;
        int n, m;
        scanf("%d%d", &n, &m);
        int i;
        for (i = 0; i < n; ++i)
        {
           int a;
            scanf("%d", &a);
            q.push(a);
            v.push(a);
        }
        while (true)
        {
            int x = q.front();
            q.pop();
            if (m == 0)  //如果m==0,则证明现在打印的是目标任务
            {
                if (x != v.top())  //如果队列中还有优先级比x高的..
                {
                   m = v.size() - 1;//下标是从0开始的
                    q.push(x);//将该任务放到队尾
                }
                else
                {
                    break;
                }
            }
            else    //如果现在的任务还不是目标任务
            {
               --m;
                if (x != v.top())
                {
                    q.push(x);
                }
                else
                {
                    v.pop();
                }
            }
        }
       printf("%d\n", n - q.size());//q.size是全部打印结束后q队列的长度
    }
    return 0;

}

D - 表达式括号匹配

 

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    stack<char>p;
    for(int i=0;s[i]!='@';i++)
    {
        if(s[i]=='(')
            p.push(s[i]);
        if(s[i]==')')
        {
            if(!p.empty())
            {
                p.pop();
            }
            else
            {
                cout<<"NO"<<endl;
                return 0;
            }
        }
    }
    if(p.empty()) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

E - 打印锯齿矩阵

 

//vector<T> vec;  
//构造一个名为vec的储存数据类型
//为T的动态数组。其中T为需要储存的数据类型
//初始时vec为空
//push_back  末尾添加一个元素
//pop_back   在末尾弹出一个元素
//size       获取长度
//clear       清空

//修改vector其中的某个元素,直接赋值,比如vec[1]=3;
/*vector的方法size()可以直接获取长度,通过[]可以直接获取其中的元素,和数组相同*/
//clear()会清空vector中内容,但是不会重新分配
/*
如果需要清空vector的内存,一种典型的方法是使用交换,
即使用一个空的vector和原来的vector进行交换,完成内存的释放
vector<int>v;
{
vector<int> x;
v.swap(x);
}
*/
/*
Clear content
Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.

A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap:

vector<T>().swap(x);   // clear x reallocating

*/

#include<iostream>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main(void)
{
    int m, n, i, j, k;
    scanf("%d%d", &n, &m);
    vector<int> ivec[10000];
    for (int k = 0; k < m; k++)
    {
        scanf("%d%d", &i, &j);
        ivec[i - 1].push_back(j);
    } 
    for (i = 0; i < n; i++)
    {
        if (!ivec[i].size())
        {
            printf("\n");
            continue;
        }
        for (j = 0; j < ivec[i].size()-1; j++)
        {
            printf("%d ", ivec[i][j]);
        }
        printf("%d", ivec[i][j]);
        printf("\n");
    }
    return 0;
}

F - 队列安排

 

#include<bits/stdc++.h>
#define LL long long
using namespace std;
int a[100010][4];//a[][1]表示本身,a[][2]表示左边的数,a[][3]表示右边的数 
int main(){
	int n;
	scanf("%d",&n);
	int k,p,s;
	s=1;//开头元素 
	a[1][1]=1;
	for(int i=2;i<=n;i++){
		scanf("%d%d",&k,&p);
		a[i][1]=i;
		if(p==0){//左边 
			a[a[k][2]][3]=i,a[i][2]=a[k][2];
			a[i][3]=k,a[k][2]=i;
			if(k==s) s=i;
		}
		else{//右边 
			a[a[k][3]][2]=i,a[i][3]=a[k][3];
			a[i][2]=k,a[k][3]=i;
		}
	}
	int m,x;
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		 scanf("%d",&x);
		 if(a[x][1]==0) continue;
		 a[x][1]=0;
		 a[a[x][2]][3]=a[x][3];
		 a[a[x][3]][2]=a[x][2];
		 n--;//剩余几个人
		 if(x==s) s=a[x][3]; 
	}
	for(int i=1;i<=n;i++){
		printf("%d ",a[s][1]);
		s=a[s][3];
	}
	printf("\n");
	return 0;
}	

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值