2014北理上机-最近目录文件

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;
/*
    1.系统中有最近打开文件的记录,先用整数表示打开的文件名,且只显示最近3个打开的文件,输出文件序列。
        eg:
        输入:1 输出:1
        输入:2 输出:2,1
        输入:3 输出:3,2,1
        输入:4 输出:4,3,2
        输入:1 输出:1,4,3
        输入:4 输出:1,4,3 //注:没有改变顺序
        输入:3 输出:1,4,3
*/

int find(int* a, int num, int t){
    for(int i =0; i<t; i++){
        if(a[i] == num)
            return 1;
    }
    return 0;
}

int main(){

    int num, a[3];
    memset(a, -1, sizeof(a));
    int t = 0;  //数字数量
    int k = 0;  //目录中是否存在

    while(1){
        cout << "输入:";
        cin >> num;

        //查找是否存在
        if(t != 0)
            k = find(a, num, t);

        //最近目录中不存在num
        if(k == 0){
            if(t < 3)t++;
            for(int i=t-1; i>0; i--){
                a[i] = a[i-1];
            }
            a[0] = num;
        }

        //输出目录
        cout << "输出:" ;
        for(int i=0; i<t; i++){
            cout << a[i] << ' ' ;
        }
        cout << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值