89 lines
2.1 KiB
C++
89 lines
2.1 KiB
C++
#include <stdio.h>
|
||
#define number 11
|
||
#define block 3
|
||
#include <iostream>
|
||
using namespace std;
|
||
int opt(int a[])
|
||
{
|
||
int i, j, zero = 0, one = 0, two = 0, q = 0;
|
||
int b[block];
|
||
for (i = 0; i < block; i++)
|
||
{
|
||
b[i] =-1; //空为-1
|
||
}
|
||
for (i = 0; i < number; i++)
|
||
{
|
||
if (i < 3)
|
||
{
|
||
// printf("缺页:%d\n", a[i]);
|
||
q++;
|
||
for (j = 0; j < block; j++)
|
||
{
|
||
if (b[j] == -1)
|
||
{
|
||
b[j] = a[i];
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (b[0] != a[i] && b[1] != a[i] && b[2] != a[i])
|
||
{
|
||
// printf("缺页:%d\n", a[i]);
|
||
q++;
|
||
for (int t = i + 1; t < number; t++)
|
||
{
|
||
if (b[0] == a[t])
|
||
zero = 1;
|
||
else if (b[1] == a[t])
|
||
one = 1;
|
||
else if (b[2] == a[t])
|
||
two = 1;
|
||
if (zero == 1 && one == 1 && two == 0)
|
||
{
|
||
b[2] = a[i];
|
||
break;
|
||
}
|
||
else if (zero == 1 && one == 0 && two == 1)
|
||
{
|
||
b[1] = a[i];
|
||
break;
|
||
}
|
||
else if (zero == 0 && one == 1 && two == 1)
|
||
{
|
||
b[0] = a[i];
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
// else
|
||
// printf("%d不缺页。\n", a[i]);
|
||
}
|
||
for (int f = 0; f < block; f++)
|
||
{
|
||
printf("%d\t", b[f]);
|
||
}
|
||
printf("\n");
|
||
zero = 0;
|
||
one = 0;
|
||
two = 0;
|
||
}
|
||
return q;
|
||
}
|
||
|
||
int main()
|
||
{
|
||
int i, o, l=0,f=0;
|
||
int a[number];
|
||
printf("请输入:\n");
|
||
for (i = 0; i < number; i++)
|
||
{
|
||
cin>>a[i];
|
||
}
|
||
printf("opt页面置换算法:\n");
|
||
o = opt(a);
|
||
printf("缺页%d次,置换%d次。\n\n\n", o, o - block);
|
||
return 0;
|
||
}
|