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.
Up to 100 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 END.
Report either Hit or Miss 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%
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
这个题读不懂,一点不懂,计算机原理这门课抢了两学期没抢到这门课,我能怎么办,我也是很绝望啊!要说有注意的地方就是END的判断了,不能判断单独的E,也不能判断单独的D,也不能挑出ED判断,可以判断第二个N。
代码实现:
#include
#include
#include
#include
#include
#include
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=100005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
int main()
{
int ans[1024];
char map[15];
int cnt=0,sum=0,i,j,k,temp,x;
mset(ans,-1);
while(cin>>map)
{
if(map[1]=='N')
{
printf("Hit ratio = %.2f%%\n",cnt*100.0/sum);
break;
}
sscanf(map,"%x",&temp); //输入字符串转换为十六进制,赋值给temp
temp-=temp%16;
x=temp%1024;
x-=x%16;
if(ans[x]!=temp)
{
cout<<"Miss"<