AB两个人玩一个游戏。先任意给定一个字母串,每个人每次从中删掉任意一个字母,两个人轮流进行,如果剩余的字母序列是严格单调递增的(按字典序a < b < c <....<z),则这个人胜利。
1 模拟这个游戏,输入任意字符串(小写),能判断输赢
1 模拟这个游戏,输入任意字符串(小写),能判断输赢
2 输入一字符串,如果两个人都足够聪明(即如果有赢的方案,都不会选输的方案 ),甲先开始,判断甲能赢么?
今日博客:
http://blog.csdn.net/flyyyri/article/details/5084981http://blog.csdn.net/woshinia/article/details/8542221
第二題不會,先解決第一題。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int judgewin(char * string,int length)
{
int i=0;
char temp,curr;
while(string[i]==0)
i++;
temp=string[i];
for(i++;i<length;i++)
{
curr=string[i];
if(curr!=0)
{
if(temp>=curr)
break;
temp=curr;
}
}
return i==length;
}
int main()
{
char *play;
int i,j,t,length;
printf("Please enter the test string:\n");
play=(char*)malloc(sizeof(char)*100);
scanf("%s",play);
length=strlen(play);
srand((unsigned)time(NULL));
int ref[length];
for(i=0;i<length;i++)
ref[i]=1;
j=i-1;
j=j%2;
while(--i)
{
while(ref[t=rand()%length]!=1)
;
ref[t]=0;
if(i%2==j)
printf("The first player ");
else
printf("The second player ");
printf("delete%d\n",t);
play[t]=0;
if(judgewin(play,length))
break;
}
if(i%2==j)
printf("The first player wins!");
else
printf("The second player wins!");
}