文件最近替换算法

来源:互联网 发布:idea uml生成java代码 编辑:程序博客网 时间:2024/05/16 07:11
#include <iostream>
#include <deque>
using namespace std;
void main(void)
{
deque<int> d;
cout<<"input:";
int n;
while(cin>>n)
{
if(d.size()==0)//里面没有元素,直接插入
{
d.push_front(n);
cout<<"output:"<<d[0]<<endl;
}
else if(d.size()==1)//里面已经有了一个元素
{
if(d[0]!=n)//若要插入元素和已有元素不等,则插入
{
d.push_front(n);
cout<<"output:"<<d[0]<<" "<<d[1]<<endl;
}
else
{//若要插入元素和已有元素相等,则不插入
cout<<"output:"<<d[0]<<endl;
}
}
else if(d.size()==2)
{
if(d[0]!=n&&d[1]!=n)//要插入的元素和已有的个元素均不等
{
d.push_front(n);
cout<<"output:"<<d[0]<<" "<<d[1]<<" "<<d[2]<<endl;
}
else
{
if(d[0]==n)
cout<<"output:"<<d[0]<<" "<<d[1]<<endl;
else if(d[1]==n)
{
int temp;
temp=d[1];
d[1]=d[0];
d[0]=temp;
cout<<"output:"<<d[0]<<" "<<d[1]<<endl;
}
}
}
else if(d.size()>=3)
{
if(d[0]!=n&&d[1]!=n&&d[2]!=n)
{//插入元素和前三个元素均不相同
d.push_front(n);
cout<<"output:"<<d[0]<<" "<<d[1]<<" "<<d[2]<<endl;
}
else//否则三个中肯定有一个和n相同
{
if(d[0]==n)
cout<<"output:"<<d[0]<<" "<<d[1]<<" "<<d[2]<<endl;
else if(d[1]==n)
{
int temp;
temp=d[1];
d[1]=d[0];
d[0]=temp;
cout<<"output:"<<d[0]<<" "<<d[1]<<" "<<d[2]<<endl;
}
else if(d[2]==n)
{
int temp;
temp=d[2];
d[2]=d[1];
d[1]=d[0];
d[0]=temp;
cout<<"output:"<<d[0]<<" "<<d[1]<<" "<<d[2]<<endl;
}
}
}
cout<<"input:";
}
}
0 0