POJ1250解题报告

Tanning Salon
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7478 Accepted: 4022

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. 

Input

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. 

Output

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<iostream>
#include<cstring>
using namespace std;
int main()
{
     int i,j,t,n,l,s,num,temp1,temp2;
     while (cin>>n,n!=0)
{
char in[21],out[40],a[40];
t=0;s=0;num=0;
        scanf("%s",a);
memset(in,'0',sizeof(in)); 
memset(out,'0',sizeof(out)); 
l=strlen(a);
for (i=0;i<l;i++)
{
temp1=temp2=1;
for (j=0;j<21;j++)
{
if (a[i]==in[j]) {temp1=0;}
}
if (temp1==1) {in[s]=a[i];s++;}
if (temp1==0) {out[t]=a[i];t++;s--;}
   for (j=0;j<t;j++)
{
if (a[i]==out[j]) {temp2=0;}
}
if (s>n && temp2!=0) {num++;}
}
if (num==0) {cout<<"All customers tanned successfully."<<endl;}
else {cout<<num<<" customer(s) walked away."<<endl;}    
}
return 0;
}

本题题意:一个店,只有n个位置,给出一串顾客进出店的顺序,如果满座了,还有顾客进入,就算损失一名客户,你写一个程序,计算损失了多少个乘客

我的做法是,如果有顾客进入,就存进in数组里,有人出去就放进out数组里,判断进入的字符,是进店还是出去,如果满座还有人进店就num++,但是得注意所,满座进人时那人出去的时候不能算入num中,每次进行判断时候得进行in,out数组的清零,不然就会出现数据不对,我试了很多次,后来看了discuss才发现这个问题,说句实话这题我错了1个月了,断断续续的今天才完成,下面是另一种做法

#include <stdio.h>  
#include <string.h>  
#include <algorithm>  
using namespace std;  
  
char s[100005];  
  
int main()  
{  
    int n;  
    while(~scanf("%d",&n),n)  
    {  
        int l=0,i,j,flag,len;  
        scanf("%s",s);  
        len = strlen(s);  
        char z[25]; 
        memset(z,'#',sizeof(z));  
        for(i=0; i<len; i++)  
        {  
            flag=0;  
            for(j=1; j<=n; j++)
                if(z[j]==s[i])  
                {  
                    z[j]='#';  
                    flag=1;  
                    break;  
                }  
            if(flag==0)  
                for(j=1; j<=n; j++)  
                    if(z[j]=='#')  
                    {  
                        z[j]=s[i];  
                        break;  
                    }  
            if(j>n)
                l++;  
        }  
        if(l==0)  
            printf("All customers tanned successfully.\n");  
        else  
            printf("%d customer(s) walked away.\n",l/2);  
    }  
    return 0;  
}  

这种做法是网上一位大神的做法,大家也可以看下。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值