UVa 489
题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错,
你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜过的单词也算出错,
给定计算机的单词以及猜测序列,判断玩家赢了(You win)、输了(You lose)、放弃了(You chickened out)
/* UVa 489 HangmanJudge --- 水题 */ #include <cstdio> #include <cstring> int kase; char a[105], b[105]; int len1, len2; int left, chance; int win, lose; /* 猜字母ch */ void guess(char ch){ bool bad = 1; //默认猜错 for (int i = 0; i < len1; ++i){ //若相等则猜对 if (ch == a[i]){ --left; bad = 0; //更改猜错标记 a[i] = ‘ ‘; //更改此处字符,下次再猜算猜错 } }//for(i) if (bad){ --chance; } if (!left){ win = 1; } else if (!chance){ lose = 1; } } int main() { #ifdef _LOCAL freopen("D:\\input.txt", "r", stdin); #endif while (scanf("%d", &kase) == 1 && kase != -1){ scanf("%s%s", a, b); win = lose = 0; //还没赢也还没输 chance = 7; //剩余7次机会 left = len1 = strlen(a); //剩余left个字母还没猜对 len2 = strlen(b); for (int i = 0; i < len2; ++i){ guess(b[i]); //猜字母 if (win || lose){ break; } } printf("Round %d\n", kase); if (win){ printf("You win.\n"); } else if (lose){ printf("You lose.\n"); } else{ printf("You chickened out.\n"); } }//while(scanf) return 0; }
时间: 2024-10-27 10:57:40