操作系统LRU算法(最近最少使用算法)

操作系统LRU算法(最近最少使用算法)

操作系统LRU算法(最近最少使用算法)_第1张图片

提交代码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public Main() {
    }

    static void upData(HashMap<Integer, Integer> map, int num) {
        Iterator var2 = map.keySet().iterator();

        while(var2.hasNext()) {
            Integer i = (Integer)var2.next();
            if (num != i) {
                map.put(i, (Integer)map.get(i) + 1);
            }
        }

    }

    static void delete(HashMap<Integer, Integer> map) {
        Pair t = null;
        boolean flag = false;
        Iterator var3 = map.keySet().iterator();

        while(var3.hasNext()) {
            Integer i = (Integer)var3.next();
            if (!flag) {
                t = new Pair(i, (Integer)map.get(i));
                flag = true;
            } else if (t.priortiy < (Integer)map.get(i)) {
                t = new Pair(i, (Integer)map.get(i));
            }
        }

        map.remove(t.num);
    }

    static void Travse(HashMap<Integer, Integer> map) {
        Iterator var1 = map.keySet().iterator();

        while(var1.hasNext()) {
            Integer i = (Integer)var1.next();
            System.out.print(i + " " + map.get(i) + " || ");
        }

        System.out.println();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("输入页面数:");
        int numPage = in.nextInt();
        System.out.println("输入内存数:");
        int numMemory = in.nextInt();
        System.out.println("输入内存:");
        int cnt = 0;
        Map<Integer, Integer> map = new HashMap();
        int[] a = new int[numPage + 10];

        for(int i = 0; i < numPage; ++i) {
            int num = in.nextInt();
            if (map.size() < numMemory) {
                if (!map.containsKey(num)) {
                    ++cnt;
                }

                map.put(num, 0);
                upData((HashMap)map, num);
            } else if (!map.containsKey(num)) {
                map.put(num, 0);
                upData((HashMap)map, num);
                delete((HashMap)map);
                ++cnt;
            } else {
                map.put(num, 0);
                upData((HashMap)map, num);
            }
        }

        System.out.printf("F' = %.1f%%", (double)cnt / (double)numPage * 100.0D);
    }
}

你可能感兴趣的:(操作系统的学习与提升,算法,java,蓝桥杯)