看完例题思路后,学习例题的自顶向下逐步求精法,然后自己实现代码,运行比例题稍稍快。。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
char s[100], s2[100];
int left, chance;//记录还需猜的单词与剩余猜的次数
int n;
int win, lose;
int d;
void guess(char s2[]){
for (int i = 0; i < strlen(s2); i++){
d = 0;
for (int j = 0; j < strlen(s); j++){
if (s2[i] == s[j]){
s[j] = ' ';
left--;
d = 1;
}
}
if (!d) chance--;
if (!chance){
lose = 1; break;
}
if (!left){
win = 1; break;
}
}
}
int main(){
while (scanf("%d%s%s",&n, s, s2) == 3 && n != -1){
printf("Round %d\n", n);
win = lose = 0;
left = strlen(s);
chance = 7;
guess(s2);
if (win) printf("You win.\n");
else if (lose) printf("You lose.\n");
else printf("You chickened out.\n");
}
return 0;
}