笔试 9.27

网易互娱 文件系统


import java.util.*;

public class Main {
    private static int count =0;
    private static Queue<Integer> queue = new ArrayDeque<>();
    private static Map<Integer,String> hashmap = new HashMap<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();


        while(loop>0){
            int m = sc.nextInt();
            String rubish = sc.nextLine();
            while(m>0) {
                String[] s = sc.nextLine().split(" ");
                if(s[0].equals("open")) open(s);
                else if(s[0].equals("dup")) dup(s);
                else if(s[0].equals("dup2")) dup2(s);
                else if(s[0].equals("query")) query(s);
                else if(s[0].equals("close")) close(s);
                m--;
            }
            hashmap = new HashMap<>();
            count =0;
            loop--;
        }

    }

    public static void open(String[] s){
        if(!queue.isEmpty()){
            int index = queue.poll();
            System.out.println(index);
            hashmap.put(index,s[1]);
        }else{
            System.out.println("???"+count);
            hashmap.put(count,s[1]);
            count++;
        }
    }

    public static void dup(String[] s){
        String file = hashmap.get(Integer.valueOf(s[1]));
        if(!queue.isEmpty()){
            int index = queue.poll();
            hashmap.put(index,file);
            System.out.println(index);
        }else{
            hashmap.put(count,file);
            System.out.println(count);
            count++;

        }


    }

    public static void dup2(String[] s){

        int fd = Integer.valueOf(s[1]);
        int newfd = Integer.valueOf(s[2]);
        String file = hashmap.get(fd);
        if(file!=null)  hashmap.put(newfd,file);
    }



    public static void close(String[] s){
        int index = Integer.valueOf(s[1]);
        queue.add(index);
        hashmap.remove(index);
    }

    public static void query(String[] s){
        int index = Integer.valueOf(s[1]);
        String file = hashmap.get(index);
        System.out.println(file);
    }
}

样例

2
10
open libc.so
open libm.so
open libdl.so
dup 2
dup2 0 2
close 0
query 1
query 2
query 3
open log.txt
14
open output.txt
dup2 0 1000000
query 0
query 1000000
close 0
open output2.txt
dup2 0 100000
query 0
query 1000000
close 0
open 1.txt
dup 100000
query 1
query 0

# 输出
0
1
2
3
libm.so
libc.so
libdl.so
0
0
output.txt
output.txt
0
output2.txt
output.txt
0
1
output2.txt

1.txt


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值