这道题比较有意思的是提前输出,比如算例:
1
a
abcdfg
输出应该是win
算法很简单,找到相同的字符就把它变0,变空好像出了问题,这里写的0,然后统计就好了,不过提前输出的条件要注意strlen和flag变量是否相等,相等立马退出
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char a1[100];
char a2[100];
void oper(char *a1,char *a2)
{ int lose=0;int flag=0;int p=1;
for(int i=0;i<strlen(a2)&&p;i++)
{ int flag1=0;
for(int j=0;j<strlen(a1)&&p;j++)
{
if(a2[i]==a1[j]) {a1[j]='0';flag1++;flag++;}
if(strlen(a1)==flag&&lose<7) {printf("You win.\n");p=0;break;}
}
if(flag1==0) {lose++;}
}
if(lose>=7) {printf("You lose.\n");}
//if(strlen(a1)==flag&&lose<7) {printf("You win.\n");}
if(strlen(a1)!=flag&&lose<7) {printf("You chickened out.\n");}
}
int main()
{//int first=1;
while(1)
{ int T=1;
memset(a1,'\0',sizeof(a1));memset(a2,'\0',sizeof(a2));
scanf("%d",&T);//if(first) {printf("\n");first=0;}
if(T==-1) {break;}
printf("Round %d\n",T);
scanf("%s%s",a1,a2);
oper(a1,a2);
}
return 0;
}