2014
1.1. 系统中有最近打开文件的记录,现用整数表示打开的文件名,且只 显示最近 显示最近 3个打开的文件 ,输出文件序列 输出文件序列 .
示例 : 输入 :1 输出 :1
输入 :2 输出 :2,1
输入 :3 输出 :3,2,1
输入 :4 输出 :4,3,2
输入 :1 输出 :1,4,3
输入 :4 输出 :1,4,3
输入 :3 输出 :1,4,3
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int x,a[3]={0},count=0;
while(scanf("%d",&x)==1)
{
if(count==0)
{
count++;
a[0]=x;
cout<<a[0]<<"\n"<<endl;
}
else if(count==1)
{
if(x==a[0])
{
cout<<a[0]<<"\n"<<endl;
continue;
}
else
{
count++;
a[1]=x;
cout<<a[1]<<","<<a[0]<<"\n"<<endl;
}
}
else if(count==2)
{
if(x==a[0]||x==a[1])
{
cout<<a[1]<<","<<a[0]<<"\n"<<endl;
continue;
}
else
{
count++;
a[2]=x;
cout<<a[2]<<","<<a[1]<<","<<a[0]<<"\n"<<endl;
}
}
else
{
if(x==a[0]||x==a[1]||x==a[2])
{
cout<<a[2]<<","<<a[1]<<","<<a[0]<<"\n"<<endl;
continue;
}
else
{
a[0]=a[1];
a[1]=a[2];
a[2]=x;
cout<<a[2]<<","<<a[1]<<","<<a[0]<<"\n"<<endl;
}
}
}
return 0;
}
2. 在第一题基础上 在第一题基础上 ,稍作改动 稍作改动 ,显示最新打开的文件 显示最新打开的文件 .
示例 : 输入 :1 输出 :1
输入 :2 输出 :2,1
输入 :3 输出 :3,2,1
输入 :4 输出 :4,3,2
输入 :1 输出 :1,4,3
输入 :4 输出 :4,1,3
输入 :3 输出 :3,4,1
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int x,a[3]={0},count=0;
while(scanf("%d",&x)==1)
{
if(count==0)
{
count++;
a[0]=x;
cout<<a[0]<<"\n"<<endl;
}
else if(count==1)
{
if(x==a[0])
{
cout<<a[0]<<"\n"<<endl;
continue;
}
else
{
count++;
a[1]=x;
cout<<a[1]<<","<<a[0]<<"\n"<<endl;
}
}
else if(count==2)
{
if(x==a[0])
{
a[0]=a[1];
a[1]=x;
cout<<a[1]<<","<<a[0]<<"\n"<<endl;
continue;
}
else if(x==a[1])
{
cout<<a[1]<<","<<a[0]<<"\n"<<endl;
continue;
}
else
{
count++;
a[2]=x;
cout<<a[2]<<","<<a[1]<<","<<a[0]<<"\n"<<endl;
}
}
else
{
if(x==a[0])
{
a[0]=a[1];
a[1]=a[2];
a[2]=x;
cout<<a[2]<<","<<a[1]<<","<<a[0]<<"\n"<<endl;
continue;
}
else if(x==a[1])
{
a[1]=a[2];
a[2]=x;
cout<<a[2]<<","<<a[1]<<","<<a[0]<<"\n"<<endl;
continue;
}
else if(x==a[2])
{
cout<<a[2]<<","<<a[1]<<","<<a[0]<<"\n"<<endl;
continue;
}
else
{
a[0]=a[1];
a[1]=a[2];
a[2]=x;
cout<<a[2]<<","<<a[1]<<","<<a[0]<<"\n"<<endl;
}
}
}
return 0;
}
3. 求广义表的深度 求广义表的深度 (实际就是括号匹配 ), 示例 :输入 (c,((d,e),f),h) 输出: 3
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int count=0,i=0,max=0;
char ch;
cout<<"please input string (在字符串结尾输入#,代表字符串结束)"<<endl;
while(cin>>ch)
{
if(ch=='(')
{
i++;
}
else if(ch==')')
{
if(max<i) max=i;
i--;
}
else if(ch=='#')
{
if(i==0)
cout<<"the depth is "<<max<<endl;
max=0;
}
}
return 0;
}