上机实验2014

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值