赞
踩
- #include<iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int arr[10];
- void getrandom();
- int FIFO(int length);
- int OPA(int length);
- int LRU(int length);
- int main()
- {
- getrandom();
- for (int i = 0; i < 10; i++)
- {
- cout << arr[i]<<' ';
- }
- cout << endl;
- int n;
- cin >> n;
- cout << FIFO(n) << endl;
- cout << OPA(n) << endl;
- cout << LRU(n) << endl;
- return 0;
- }
-
- void getrandom()
- {
- for (int i = 0; i < 10; i++)
- {
- arr[i]= rand() % 9 + 1;
- }
- }
-
- int FIFO(int length)
- {
- int* page;
- int* sequence;
- page = new int[length];
- sequence = new int[length];
- for (int i = 0; i < length; i++)
- {
- page[i] = 10;
- sequence[i] = 0;
- }
- int j = 0;
- int num = 0;
- while (j < 10)
- {
- int flag = 0;
- for (int i = 0; i < length; i++)
- {
- if (page[i] == arr[j])
- {
- flag++;
- break;
- }
- }
- if (flag == 0)
- {
- int fl = 0;
- int first = sequence[0];
- for (int i = 0; i < length; i++)
- {
- if (sequence[i] < first)
- {
- first = sequence[i];
- fl = i;
- }
- }
- page[fl] = arr[j];
- sequence[fl]++;
- num++;
- }
- j++;
- }
- return num;
- }
-
- int OPA(int length)
- {
- int* page;
- page = new int[length];
- for (int i = 0; i < length; i++)
- {
- page[i] = 10;
- }
- int* note;
- note = new int[length];
- for (int i = 0; i < length; i++)
- {
- note[i] = 0;
- }
- int num=0;
- int j = 0;
- while (j < 10)
- {
- int flag = 0;
- for (int i = 0; i < length; i++)
- {
- if (page[i] == arr[j])
- {
- flag++;
- break;
- }
- }
- if (flag == 0)
- {
- for (int i = 0; i < length; i++)
- {
- for (int t = j; t < 10; t++)
- {
- if (page[i] == arr[t])
- {
- note[i] = t;
- break;
- }
- }
- }
- int fl = 0;
- for (int i = 0; i < length; i++)
- {
- if (note[i] == 0)
- {
- fl++;
- page[i] = arr[j];
- num++;
- break;
- }
- }
- if (fl == 0)
- {
- int fg = note[0];
- int a = 0;
- for (int i = 0; i < length; i++)
- {
- if (note[i] > note[0])
- {
- fg = note[i];
- a = i;
- }
- }
- page[a] = arr[j];
- num++;
- }
- }
- j++;
- }
- return num;
- }
-
- int LRU(int length)
- {
- int* page;
- page = new int[length];
- int* count;
- count = new int[length];
- for (int i = 0; i < length; i++)
- {
- page[i] = 10;
- count[i] = 0;
- }
- int num = 0;
- int j = 0;
- while (j < 10)
- {
- int flag = 0;
- for (int i = 0; i < length; i++)
- {
- if (page[i] == arr[j])
- {
- flag++;
- break;
- }
- }
- if (flag == 0)
- {
- int min = count[0];
- int fl = 0;
- for (int i = 0; i < length; i++)
- {
- if (count[i] < min)
- {
- min = count[i];
- fl = i;
- }
- }
- page[fl] = arr[j];
- count[fl] = j;
- num++;
- }
- j++;
- }
- return num;
- }
这只是最简单的实现,不一定完全正确,有误之处,多多指教
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。