码迷,mamicode.com
首页 > 其他好文 > 详细

难题Tanning Salon

时间:2015-02-16 17:01:37      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:数据   初学者   

这是一道关于客人进出问题的题,要我觉得也是一道“开关”类型的题目,这道题反正对我这个初学者来说比较难,这道题的大体意思是:一横数据包括两部分,一个数字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;
}

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

难题Tanning Salon

标签:数据   初学者   

原文地址:http://blog.csdn.net/unusualnow/article/details/43852345

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!