POJ1250解题报告
2015-09-13 19:41
309 查看
题意
题意:这个题的意思比较复杂。我大致解释一下旅馆房间有限,先到先入住,后到后入住,用ABCDEF等字符代表单个人,第一次出现代表入住,第二次出现代表离开
这样我们就可以读懂题了,
比如input的
2 ABBAJJKZKZ
代表有两间房,一个一个读过去,
第一个是A,房给他一间,还剩一间
第二个是B ,房给他一件,没有了
第三个是B,B退房了,所以房又有一间了
第四个是A,A退房了,房又有两间了
… 依次循环,最后看有几个人来旅馆了没地方住离开了
思路
就像我分析的题目意思那样我设置了两个存储结构,第一个是char Intemp[],存储已经入住的旅客
第二个是char Outtemp[],存储离开的旅客
我把每次读进来的字符分为多种
第一种:在Intemp里面的旅客,这样我就把客房数(remain)-1;把Intemp里面的姓名抹掉
第二种:在Outtemp里面的旅客,把其在outtemp里名字抹掉
第三种,刚来能住店的旅客,把它存进Intemp里面
第四种,客满离开的旅客,把它存进Outtemp里面
代码
#include <iostream> #include <string> char Intemp[1000]; //存储入住的客人名 char Outtemp[1000]; //存储离开的客人名 using namespace std; int Isin(char a){ //在Intemp里面查找客人名 for(int i=0;i<1000;i++){ if(Intemp[i]==a){ return 1; } } return 0; } int Isout(char a){ //在outtemp里面查找客人名 for(int i=0;i<1000;i++){ if(Outtemp[i]==a){ return 1; } } return 0; } void InsertIn(char a){ Intemp[strlen(Intemp)]=a; } void Insertout(char a){ Outtemp[strlen(Outtemp)]=a; } void LeaveIn(char a){ for(int i=0;i<1000;i++){ if(Intemp[i]==a){ Intemp[i]=' '; } } } void LeaveOut(char a){ for(int i=0;i<1000;i++){ if(Outtemp[i]==a){ Outtemp[i]=' '; } } } int main() { int n; string s; while(cin>>n>>s && n!=0){ int remain = n;int leave=0; for(int i=0;i<s.length();i++){ if(Isin(s[i])){ remain++; LeaveIn(s[i]); continue; } if(Isout(s[i])){ LeaveOut(s[i]); continue; } if(Isin(s[i])==0 && Isout(s[i])==0){ if(remain==0){ leave++; Insertout(s[i]); continue; } else{ remain--; InsertIn(s[i]); } } } if(leave==0){ cout<<"All customers tanned successfully."<<endl; } else cout<<leave<<" customer(s) walked away."<<endl; } return 0; }
相关文章推荐
- drools入门(二)
- JQuery中$.ajax()方法参数详解
- Strurts2下导入Excel文件数据
- C 语言中的指针和内存泄漏
- hdu 5441 Travel(并查集)
- c++求数组长度
- 对socket的理解
- 读文件
- 在制作水晶报表时遇到此错误未处理System.IO.FileNotFoundException HResult=-2147024894
- 学习JAVA,从认识关键字开始
- JSP之POST与GET的区别
- java基础知识
- MySQL数据库事务隔离级别(Transaction Isolation Level)
- 动态路径规划(二)
- hdu 5444 Elven Postman(模拟)
- Ubuntu 基础操作
- 当模板函数遇上数组参数
- 链表的操作
- MBProgressHUD的使用
- 收获