2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A Cache Simulator
2017-09-24 19:42
337 查看
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.
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.
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.
2017
ACM-ICPC 亚洲区(南宁赛区)网络赛
这个题读不懂,一点不懂,计算机原理这门课抢了两学期没抢到这门课,我能怎么办,我也是很绝望啊!要说有注意的地方就是END的判断了,不能判断单独的E,也不能判断单独的D,也不能挑出ED判断,可以判断第二个N。
代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#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"<<endl;;
ans[x]=temp;
}
else
{
cout<<"Hit"<<endl;
cnt++;
}
sum++;
}
return 0;
}
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 linesof 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 foreach 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%
题目来源
2017ACM-ICPC 亚洲区(南宁赛区)网络赛
这个题读不懂,一点不懂,计算机原理这门课抢了两学期没抢到这门课,我能怎么办,我也是很绝望啊!要说有注意的地方就是END的判断了,不能判断单独的E,也不能判断单独的D,也不能挑出ED判断,可以判断第二个N。
代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#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"<<endl;;
ans[x]=temp;
}
else
{
cout<<"Hit"<<endl;
cnt++;
}
sum++;
}
return 0;
}
相关文章推荐
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 G. Finding the Radius for an Inserted Circle
- 【计算几何】【圆反演】计蒜客17314 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 G. Finding the Radius for an Inserted Circle
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B题 离散化+贪心
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Train Seats Reservation
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Frequent Subsets Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J Minimum Distance in a Star Graph 广度优先搜索
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J.Minimum Distance in a Star Graph
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F. Overlapping Rectangles(扫描线)
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:L. The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 H A Cache Simulator
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L. The Heaviest Non-decreasing Subsequence Problem(最长非减子序列+思路)
- 计蒜客 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A Weather Patterns
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 GSM Base Station Identification 线性变换||计算几何
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem 最长不下降序列
- Frequent Subsets Problem 进制运算 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- Finding the Radius for an Inserted Circle 笛卡尔定理 - 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)英语阅读网络竞赛