poj 1250 Tanning Salon

一道简单题,思路很简单,用一个数组记住是否有客人来到。用字符 c-'A' 将A~Z映射到数组的下标0~25

这个是很久以前写的

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char c;
    int n,flag,bed[26],aaa;
    while(1)
    {
            scanf("%d",&n);
        if(n==0)    exit(-1);
        flag=0;aaa=0;
        memset(bed,0,26*sizeof(int));
        getchar();
        while(scanf("%c",&c),c!='\n')
        {
        if(flag<n)
        if(bed[c-65]==-1)    ;
            else if(bed[c-65]==0)    {bed[c-65]=1;flag++;}
            else    {bed[c-65]=0;flag--;}
        else
            if(bed[c-65]==-1)    ;
            else if(bed[c-65]==1)    {bed[c-65]=0;flag--;}
            else    {bed[c-65]=-1;aaa++;}
        
        }
        if(aaa==0)    printf("All customers tanned successfully.\n");
        else    printf("%d customer(s) walked away.\n",aaa);
        
    }
}

上面程序对于没有床位的顾客直接让数组中对应元素下标为-1,以后在遇到就不用处理了。

 

其实可以更简单的,因为题中说“Customers who leave without tanning always depart before customers who are currently tanning.”。看下面这样getchar()就可以了

#include<stdio.h>
#include<string.h>
int main()
{
    char c;
    int bed,flag[30],n,result;
    while(scanf("%d",&bed)==1 && bed!=0)
    {
        n=0;result=0;
        memset(flag,0,sizeof(flag));getchar();
        while((c=getchar())!='\n')
        {
            if(n<bed){
                if(!flag[c-'A']) {flag[c-'A']=1;n++;}
                else    { flag[c-'A']=0;n--; }
            }
            else
                if(flag[c-'A'])    { flag[c-'A']=0;n--; }
                else {getchar();result++;}
        }
        if(result==0) printf("All customers tanned successfully.\n");
        else printf("%d customer(s) walked away.\n",result);
    }
}

 

posted @ 2013-05-14 17:04  piaomiao1314  阅读(175)  评论(0编辑  收藏  举报