队列 queue

文章描述了四个编程问题:队列的基本操作如push、pop、empty和查询,以及如何模拟队列并处理ATM取款场景、删除字符使字符串符合特定模式和排序整数数组。
摘要由CSDN通过智能技术生成
push() 在队尾插入一个元素
pop() 删除队列第一个元素
size() 返回队列中元素个数
empty() 如果队列空则返回true
front() 返回队列中的第一个元素
back() 返回队列中最后一个元素

problem 模拟队列
describe
实现一个队列,队列初始为空,支持四种操作:

1.push x – 向队尾插入一个数 x
2.pop – 从队头弹出一个数;
3.empty – 判断队列是否为空;
4.query – 查询队头元素。
现在要对队列进行 M个操作,其中的每个操作 3 和操作 4 都要输出相应的结果。

#include<iostream>
#include<queue>

using namespace std;

int main()
{
    queue<int> q;
    int n;
    cin>>n;
    while(n--)
    {
        string op;
        cin>>op;
        int x;
        if(op=="push")
        {
        cin>>x;
        q.push(x);
        }
        else if(op=="pop")
        {
            q.pop();
        }
        else if(op=="empty")
        {
            if(!q.empty())
        printf("NO\n");
        else printf("YES\n");
        }
        else  
        {
            int t=q.front();
            printf("%d\n",t);
        }
    }
    return 0;
}

problem ATM 队列
describe
N 个人(编号 1∼N),排成一队在 ATM 机前准备取钱。初始时,队列按编号升序的顺序排列。
第 i 个人需要取 Ai 元钱。一个人一次最多可以取 X元钱。

当轮到某个人取钱时,如果其需要的钱的数量大于 X,则只能先取 X 元钱,然后去队尾重新排队,等待下次轮到他取钱时,继续去取。
当一个人取够钱时,他就会拿着钱离开队列。
现在,请你确定所有人离开队列的顺序。

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

const int N = 1e5 + 10;
pair<int, int> a[N];

int main() {
  int T; 
  cin >> T;
  for (int t = 1; t <= T; t ++) {
    cout << "Case #" << t << ":";
    int n, x; cin >> n >> x;
    for (int i = 0; i < n; i ++) {
      int y; 
      cin >> y;
      a[i] = {(y - 1) / x, i + 1};
    }
    sort(a, a + n);
    for (int i = 0; i < n; i ++) cout << ' ' << a[i].second;
    cout << endl;
  }
  return 0;
}

problem fake news(easy)
describe
给定一个字符串询问能否通过删除一些字母使其变为“heidi”。
如果可以输出“YES”,不然为“NO”

#include <iostream>
#include <string>
using namespace std;
char c[5]={'h','e','i','d','i'};
int main(){
	int i,j,k,n,sum=0;
	string st;
	cin>>st;
	k=0;
	for(i=0;i<st.length();i++){
		if(st[i]==c[k]){
			sum++;
			k++;
		}
	}
	if(sum==5)cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}

problem chat room
describe
给定一个字符串使删除这个字符串的一些字符使它变成“hello”

#include<bits/stdc++.h>

using namespace std;
char s[1001];
char goal[5]={'h','e','l','l','o'};
int top=0;
int main()
{
    scanf("%s",s);
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]==goal[top])
        {
            top++;
        }
        if(top==5)
        {
            break;
        }
    }
    if(top==5)
    {
        cout<<"YES"<<endl;
    }
    else cout<<"NO"<<endl;
}

problem crunching numbers just for you
describe
你正在为销售机票的网站开发一项新功能:按能够对价格进行票分类照片你已经提取了票价,所以只有最后一步要做…

给你一个整数数组。按非降序排序。 输入输出格式 输入格式: 输入由一行空格分隔的整数组成。 第一个数字是N(1<=N<=10) ,数组的大小。下列 N个数字的数组a的所有元素(1<=ai<=100)。
输出格式: 输出排序数组的空格分隔元素。
记住,这是一个非常重要的功能,你必须确保顾客欣赏它!

#include <cstdio>
#include <algorithm>
using namespace std;
int main () {
	int a[10],n,i;
	scanf ("%d",&n);
	for (i=0;i<n;i++) scanf ("%d",&a[i]);
	sort (a,a+n);
	for (i=0;i<n;i++) printf ("%d ",a[i]);
	return 0;
}
  • 20
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值