难题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;
}

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

时间: 2024-12-13 17:06:50

难题Tanning Salon的相关文章

北大ACM题库习题分类与简介(转载)

在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------------------------------------------------------------------- acm.pku.edu.cn 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 23

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

转载 ACM训练计划

leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/problems/largest-rectang

HDU——PKU题目分类

HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201

poj题库分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea

算法做题顺序

初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj3295) (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法: (1)图的深度优先遍历和广度优先遍历. (2)最短路径算法(dijkstra,bellman-ford,floyd,heap+dijkstra) (poj1860,poj3259,p

poj循序渐进300题

主要是整理起来自己用的.网上有多个版本. 初级: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj3295) (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法: (1)图的深度优先遍历和广度优先遍历. (2)最短路径算法(dijkstra,bellman-ford,floyd,heap+dijkstr