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

java FIFO算法,纯逻辑编码
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
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(); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报