前鱼不泣 2022-03-07 12:03 采纳率: 81.8%
浏览 34
已结题

java FIFO算法,纯逻辑编码

一个一个的代替。不能用那些智能编码像map什么的就纯逻辑代替。输出像下面给出的一样。内容是20 30 10.用20 5 30 10 5 20去测试。结果5 20 10.命中不代替输出h,反之输出吗并代替。

img

  • 写回答

1条回答 默认 最新

  • EngineZhang 2022-03-07 14:09
    关注

    你这输出结果不对啊,FIFO的cache最后应该是10,5,20

        public static void main(String[] args){
            int[] cache = {20,30,10};
            int[] input = {20,5,30,10,5,20};
            System.out.println("Cache content:");
            for(int i : cache){
                System.out.print(i+" ");
            }
            System.out.println();
            System.out.println("Request sequence:");
            for(int i : input){
                System.out.print(i+" ");
            }
            System.out.println();
            String[] result = new String[6];
            System.out.println("noEvict");
            for(int i =0;i<input.length;i++){
                for(int c : cache){
                    if(input[i] == c){
                        result[i] = "h";
                    }
                }
                if(!"h".equals(result[i])){
                    result[i] = "m";
                }
            }
            int hcount = 0;
            int mcount = 0;
            for(String r : result){
                if("h".equals(r)){
                    hcount++;
                }else{
                    mcount++;
                }
                System.out.print(r);
            }
            System.out.println();
            System.out.println(hcount + " h " + mcount + " m");
            System.out.print("Cache: ");
            for(int i : cache){
                System.out.print(i+",");
            }
            System.out.println();
    
            System.out.println("evictFIFO");
            result = new String[6];
            for(int i =0;i<input.length;i++){
                for(int c : cache){
                    if(input[i] == c){
                        result[i] = "h";
                    }
                }
                if(!"h".equals(result[i])){
                    result[i] = "m";
                    for(int j=0;j<cache.length-1;j++){
                        cache[j] = cache[j+1];
                    }
                    cache[cache.length-1] = input[i];
                }
            }
            hcount = 0;
            mcount = 0;
            for(String r : result){
                if("h".equals(r)){
                    hcount++;
                }else{
                    mcount++;
                }
                System.out.print(r);
            }
            System.out.println();
            System.out.println(hcount + " h " + mcount + " m");
            System.out.print("Cache: ");
            for(int i : cache){
                System.out.print(i+",");
            }
            System.out.println();
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 已采纳回答 3月7日
  • 创建了问题 3月7日

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000