您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: