现在的位置: 首页 > 综合 > 正文

toj1923 Tanning Salon

2014年07月12日 ⁄ 综合 ⁄ 共 1106字 ⁄ 字号 评论关闭

题目链接:http://acm.tju.edu.cn/toj/showp.php?pid=1923

题目大意:给定椅子数目,给定一序列表示人来了又走,问来了马上就走(表示没有椅子了)的人数。

思路:这题目感觉像栈,但其实没有用到栈,用一个标记已经用了的椅子数目,用一个标记人是否已经占到座位,用一个变量计算走的人数,进行相应操作即可。

代码:

#include <iostream>   
#include <stack>
#include <cstdlib>
#include <cstring>
using namespace std;
bool vis[27];
int main()
{
    int i,j,k,T;
    while(cin>>T,T)
    {
        memset(vis,false,sizeof(vis));
        int cur = 0;//已经使用的凳子
        int ans = 0;//走的人
        string str;
        str.clear();  //string 和char[]同作用
        cin>>str;

        for(i=0;i<str.length();i++)
        {
           int index = str[i]-'A';//因为字符串中是一对一对的 得作标记是否已经占到座了 相对'A'进行标记 常用手法
           if(vis[index]==false)
           {
                if(cur<T)
                {
                    cur++;
                    vis[index] = true;
                }
                else
                    ans++;  //没有位子了 答案加1
            }
            else
            {
                vis[index] = false; 
                cur--;
            }
        }
        ans >>= 1;//ans肯定为偶数:别忘了除以2 因为它出现两次 这里向右移动两位
        if(ans == 0)
            cout<<"All customers tanned successfully."<<endl;
        else
            cout<<ans<<" customer(s) walked away."<<endl;
    }
    return 0;
}

抱歉!评论已关闭.