Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7478 | Accepted: 4022 |
Description
Input
Output
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;
}
这种做法是网上一位大神的做法,大家也可以看下。。。