算法规则:
其所选择的被淘汰的页面是以后永不使用,或是最长时间内不被访问的页面。
VS不愧被称作宇宙最强IDE,真TM好用,调试功能一级赞?,似乎现在爱上了它,嘤嘤嘤。
但是这是操作系统课啊,不是让你在linux上变编程吗?哈哈哈,没忍住又打开了VS!
在这里为VS打一波广告【库拽】
推荐IDE:VS2017
推荐主题:暗色调
推荐字体:consolas
推荐字号:11或18号字号(consolas会根据字体大小变粗变细,这两字号感觉最舒服)
效果:(是不是超级赞)
Code
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cstdio>
#define pause system("pause")
using namespace std;
const int maxn = 1005;
int a[maxn];
int b[maxn];
vector<int> v[maxn];
void init(int m)
{
for (int i = 0; i < m; i++) b[i] = -1;
}
int main()
{
int n;
cout << "pages num >> ";
cin >> n;
cout << "pages val >> ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
v[a[i]].push_back(i);
}
int m;
cout << "blocks num >> ";
cin >> m;
init(m);
int cnt = 0;
for (int i = 0; i < n; i++)
{
int flag = 0;
for (int j = 0; j < m; j++)
{
if (b[j] == a[i])
{
flag = 1;
v[a[i]].erase(v[a[i]].begin());
break;
}
if (b[j] == -1)
{
b[j] = a[i];
v[a[i]].erase(v[a[i]].begin());
flag = 1;
break;
}
}
if (!flag)
{
int lastinx = 0;
int lastime = -1;
for (int j = 0; j < m; j++)
{
if (v[b[j]].empty())
{
lastinx = j;
break;
}
if (v[b[j]][0] > lastime)
{
lastinx = j;
lastime = v[b[j]][0];
}
}
v[a[i]].erase(v[a[i]].begin());
printf("change:%d——>%d\n", b[lastinx], a[i]);
b[lastinx] = a[i];
cnt++;
}
}
cout << m + cnt << endl;
return 0;
}
/*
20
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
3
*/