难题Tanning Salon

这是一道关于客人进出问题的题,要我觉得也是一道“开关”类型的题目,这道题反正对我这个初学者来说比较难,这道题的大体意思是:一横数据包括两部分,一个数字n(就好像是床的数目), 后边是来的客人(用大写字母表示),字母第一次出现表示客人进来,第二次出现表示离开,一次类推,计算流失的客人数目。
刚开始做的时候一点思路都没有,题目的意思到不难,后来还好提交时候虽然错了好几次,但是,我的思路是正确的,这是我犯得错误:
1.输出时候落了标点“.”,就像这样的最好直接复制上去,这样就不会出现这样的错误了;
2.那个用来表示位置(床的数目)的数组开小了;

好了话不多说了,让我们看一下题目吧

Description
Tan Your Hide, Inc., owns several coin-operated tanning salons. Research has shown that if a customer arrives and there are no beds available, the customer will turn around and leave, thus costing the company a sale. Your task is to write a program that tells the company how many customers left without tanning.

The input consists of data for one or more salons, followed by a line containing the number 0 that signals the end of the input. Data for each salon is a single line containing a positive integer, representing the number of tanning beds in the salon, followed by a space, followed by a sequence of uppercase letters. Letters in the sequence occur in pairs. The first occurrence indicates the arrival of a customer, the second indicates the departure of that same customer. No letter will occur in more than one pair. Customers who leave without tanning always depart before customers who are currently tanning. There are at most 20 beds per salon.

For each salon, output a sentence telling how many customers, if any, walked away. Use the exact format shown below.

Sample Input

2 ABBAJJKZKZ
3 GACCBDDBAGEE
3 GACCBGDDBAEE
1 ABCBCA
0

Sample Output

All customers tanned successfully.
1 customer(s) walked away.
All customers tanned successfully.
2 customer(s) walked away.

这是我的代码

#include<stdio.h>
#include<string.h>
int main()
{
    int n, i, j, sum, flag, len;
    char a[1000], bed[30];//表示床的数目,记得要开的稍微大一点
    while(~scanf("%d", &n))
    {
        if(n==0)
        {
            break;
        }
        scanf("%s", a);
        sum=0;
        len=strlen(a);
        memset(bed, '0', sizeof(bed));
        for(i=0; i<len; i++)
        {
            flag=0;//标记 ,0时表进 
            for(j=1; j<=n; j++) 
            {
                if(a[i]==bed[j])//一进一出的条件 
                {
                    bed[j]='0';// 清空 
                    flag=8;//8时表出 
                    break;
                }
            }
            if(flag==0)
            {
                for(j=1; j<=n; j++)//如果有空闲位置,顾客就可以进来
                {
                    if(bed[j]=='0')
                    {
                        bed[j]=a[i];
                        break;
                    }
                }
            }
            if(j>n)//如果没有多余位置,要离开的人数就增加 
            {
                sum++;
            }
        }
        if(sum==0)
        {
            printf("All customers tanned successfully.\n");
        }
        else
        {
             printf("%d customer(s) walked away.\n", sum/2);   
        }
    }
    return 0;
}

哎,这就是难题了对我这个初学者来说呀!好吧,继续努力,加油吧~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值