保存用户历史操作 | 双端队列应用

Question

在许多应用类软件的开发中都需要有保存用户历史操作的功能,例如word需要存储用户的编辑操作历史,浏览器需要存储用户浏览网页的历史,搜索栏需要保存最近的搜索记录等。请编写程序存储用户最近的n条操作记录,并将其按照时间顺序(由近到远)输出。

Description

input
设置软件需要最大保留的历史操作条数n(1<=n<=50)
用户的历史操作序列(数值可能大于n)且操作序列用大写字母来表示。
output
最近的n条操作序列(若操作序列长度小于n,则全部输出)

Example

**input**
5
A B C D
**output**
D C B A

**input**
5
A B C D E F G
**output**
G F E D C

code

#include <iostream>
#include <deque>
using namespace std;

int main() {
	deque<char>c;
	char op;
	int n;
	cin >> n;

	int count = 0;
	while ((op=cin.get())!=EOF)
	{   
		if (op >= 'A' && op <= 'Z') {
			c.push_back(op);
			count++;
			if (count > n)
				c.pop_front();
		}
	}

	for (int i = 0; i < n; i++) {
		if (!c.empty) {
			cout << c.back() << " ";
			c.pop_back();
		}
		else
			break;
		
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值