<!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋體; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->
輸入格式:
第一行包含一個整數,表示測試數據的組數。每一組測試數據包含兩行,第一行是緩存區的容量 m ( m<=10000 )和引用序列的長度( 0<n<=100000 ) . 第二行包含 n 個整數,表示整個引用序列(頁面號碼範圍爲 0 到 n )。
輸出格式:
對於每一組測試數據,輸出應該包含缺頁錯誤發生的次數。
/*******************************************
程序名:最近最少使用算法 LRU
作者:許文發
時間; 2009-11-24
********************************************/
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int count=0;// 計算缺頁次數
int first=1;// 第一次輸出標誌
// 判斷是否還有空的容量, 1 代表有, 0 代表沒有
int isempty(int flash[],int m)
{
int empty=0;
for(int i=0;i<m;i++)
{
if(flash[i]==-1)
empty=1;
}
return empty;
}
//LRU 算法
void LRU(int p,int flash[],int status[],int m)
{
int i;
int max,n;
int inpage=0;
// 先判斷頁是否存在,存在則 inpage=1 ,否則 inpage=0
for(i=0;i<m;i++)
{
if(p==flash[i])
{
inpage=1;
n=i;
}
}
// 根據頁存在標誌,分情況
if(inpage)
{
status[n]=1;
}
else
{
count++;
if(isempty(flash,m))
{
for(i=0;i<m;i++)
{
if(flash[i]==-1)
{
n=i;
flash[i]=p;
status[i]=1;
break;
}
}
}
else
{
n=0;
max=status[0];
for(i=0;i<m;i++)
{
if(max<status[i])
{
max=status[i];
n=i;
}
}
status[n]=1;
flash[n]=p;
}
}
// 其他非空空間時間狀態量加 1
for(i=0;i<m;i++)
{
if(flash[i]!=-1 && i!=n)
status[i]++;
}
}
void clearfile()
{
FILE *pt;
pt=fopen("output.txt","w");
fclose(pt);
}
void mywrite(int n,int first)
{
FILE *pt;
pt=fopen("output.txt","a");
if(first)
{
fprintf(pt,"%d",n);
}
else
{
fprintf(pt,"/n%d",n);
}
fclose(pt);
}
void main()
{
clearfile();
int m,n,i,j,total; //m 表示緩衝空間容量, n 代表引用序列個數, total 表示組數
FILE *pt;
int page[100000]; // 引用序列
int flash[10000]; // 緩衝空間
int status[10000]; // 空間時間量,表示也最近未被訪問的量,大的表示未被訪問的越久 !
if(NULL==(pt=fopen("input.txt","r")))
{
cout<<"can't open input.txt!";
}
else
{
fscanf(pt,"%d",&total);
for(i=0;i<total;i++)
{
memset(flash,-1,10000);
memset(status,0,10000);
memset(page,0,100000);
fscanf(pt,"%d",&m);
fscanf(pt,"%d",&n);
count=0;
for(j=0;j<n;j++)
{
fscanf(pt,"%d",&page[j]);
}
for(int ii=0;ii<n;ii++)
{
LRU(page[ii],flash,status,m);
}
mywrite(count,first);
first=0;
}
fclose(pt);
}
}
輸入:
3
3 5
1 2 3 4 5
3 5
1 2 1 2 3
3 20
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
輸出:
5
3
12