程序设计大赛—页面置换算法(LRU)

 

<!-- /* 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章