2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 H A Cache Simulator

题目链接

Cache memories have been used widely in current microprocessor systems. In this problem, you are asked to write a program for a cache simulator. The cache has the following metrics:

  1. The cache size is 1 KB (K-byte).

  2. The cache uses the direct mapped approach.

  3. The cache line size is 16 bytes.

  4. The cacheable memory size is 256MB.

Your program will report a hit or miss when an address is given to the cache simulator. This is often called trace simulation. Initially, all of the cache lines are in the invalid state. When a memory line is first brought into the cache, the allocated cache entry transits into the valid state. Assume that a miss causes the filling of a whole cache line.

Input Format

Up to 100100 lines of address can be given in the file. Each line consists of an address given to the simulator. The address is given in hexadecimal form. We assume that each address references only one byte of data. The input file ends at the line with the word ENDEND.

Output Format

Report either HitHit or MissMiss for each of the given addresses. The last line reports cache hit ratio in percentage, that is, the number of hits divided by the number of total addresses given.

样例输入

AAAA000
00010B2
00010BA
END
样例输出

Miss
Miss
Hit
Hit ratio = 33.33%

这里写图片描述

如图,就是一个寄存器,对应的关系,因为一行是16个字节,所以1kb只能存储64个数据,这里存储的数据就是地址除以1024,我也不知道这是个啥。http://blog.csdn.net/whuzm08/article/details/51800626 看看这个。然后就是如果再次访问这个缓存器的位置,如果存储的是这个数据就是hit否则就是miss,并把数据覆盖,因为缓存器只能存储64个位置,所以找到对应位置是address/16 % 64,这题真TM的扯淡,代码非常简单,就是一个转换的问题,但这题意谁TM知道啊,听说还是台湾之前的原题,用这种垃圾题目,做着有什么意思呢?

代码如下:

#include<bits/stdc++.h>
using namespace std;
int mod = 64;
int book[100];
int main(void){
    char str[10];
    int Miss = 0,Hit = 0;
    memset(book,-1,sizeof(book));
    while(scanf("%s",str)){
        if(strcmp(str,"END") == 0)
            break;
        int x;
        sscanf(str,"%X",&x);
        int value = x/1024;
        int pos = x/16 % mod;
        if(book[pos] != value){
            ++Miss;
            printf("Miss\n");
            book[pos] = value;
        }
        else{
            printf("Hit\n");
            ++Hit;
        }
    }
    double res = ((double)Hit/(double)(Miss+Hit))*10000;
    res = round(res);//保留两位小数
    res/=100;
    printf("Hit ratio = %.2lf%%\n",res);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值