489这是题目,从别的地方抄的引用489 - Hangman JudgeTime limit: 3.000 secondsIn ``Hangman Judge, you
489 这是题目,从别的地方抄的
引用 489 - Hangman Judge Time limit: 3.000 seconds In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows: The contestant tries to solve to puzzle by guessing one letter at a time. Every time a guess is correct, all the characters in the word that match the guess will be ``turned over.'' For example, if your guess is ``o'' and the word is ``book'', then both ``o''s in the solution will be counted as ``solved.'' Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete. Each unique wrong guess only counts against the contestant once. ______ | | | O | /|\ | | | / \ __|_ | |______ |_________| If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word, the contestant loses. If the contestant has guessed all the characters of the word before the drawing is complete, the contestant wins the game. If the contestant does not guess enough letters to either win or lose, the contestant chickens out. Your task as the ``Hangman Judge'' is to determine, for each game, whether the contestant wins, loses, or fails to finish a game. Input Your program will be given a series of inputs regarding the status of a game. All input will be in lower case. The first line of each section will contain a number to indicate which round of the game is being played; the next line will be the solution to the puzzle; the last line is a sequence of the guesses made by the contestant. A round number of -1 would indicate the end of all games (and input). Output The output of your program is to indicate which round of the game the contestant is currently playing as well as the result of the game. There are three possible results: You win. You lose. You chickened out. Sample Input 1 cheese chese 2 cheese abcdefg 3 cheese abcdefgij -1 Sample Output Round 1 You win. Round 2 You chickened out. Round 3 You lose. 为了节省大家的时间,翻译了一下 Q489: Hangman Judge Hangman Judge是一个猜英文单字的小游戏(在电子字典中常会看到),游戏规则如下: 1. 答案单字写在纸上(每个字符一张纸),并且被盖起来,玩家每次猜一个英文字符(letter)。 2. 如果这个英文字符猜中(在答案的英文单字中有出现),被猜中的字符就被翻开。例如:答案是book,如果你猜o,book中的两个o就会被视为已猜中。 3. 如果这个英文字符未出现在答案的单字中,就会在hangman的图中多加一划。要完成hangman图共需7划,如下图。注意:同一个猜错的字符只能再图上画一划,例如:答案是book,第一次你猜a(未猜中)会在图上画一划,但第二次以后再猜a并不会再多画。 4. 如果在hangman图完成之前,玩家已猜中所有答案中的字符,则玩家赢(win)。 5. 如果玩家尚未猜中所有答案中的字符而hangman图完成了,,则玩家输(lose)。 6. 如果玩家在还没输赢的情况之下就不玩了,那我们说玩家胆小放弃了(chicken out). ______ | | | O | /|\ | | | / \ __|_ | |______ |_________| 你的任务就是要写一个程序根据答案及玩家输入的猜测来判断玩家是赢、输、或放弃。 Input 会有好几组测试资料,每一组有3列。第一列为一个数字n,代表第几回合,第二列为这一回合的答案,第三列为这一回合玩家输入的猜测。如果 n = -1代表输入结束。 Output 请输出每一回合及游戏结果。游戏结果只有三种可能: You win. You lose. You chickened out. 请参考sample output。 Sample input 1 cheese chese 2 cheese abcdefg 3 cheese abcdefgij -1 Sample Output Round 1 You win. Round 2 You chickened out. Round 3 You lose.
这是我写的
#include <stdio.h> #include <string.h> int main() { #ifdef LOCAL freopen("data.in","r",stdin); #endif int n,sign,i,j,k,error,l=0; char sol[105],guess[105]; while(scanf("%d",&n)&&n!=-1) { memset(sol,0,sizeof(sol)); memset(guess,0,sizeof(guess)); error=0; scanf("%s",sol); scanf("%s",guess); while(sol[l++]!='\0'); l--; for(i=0;i<105&&guess[i]!=0;i++) { sign=1; for(j=0;j<105;j++) { if(guess[i]==sol[j]) { sol[j]=0; sign=0; l--; } } if(sign) { for(j=i-1;j>=0;j--) if(guess[j]==guess[i]) sign=0; if(sign) error++; } if(l==0||error>=7) break; } if(l==0) printf("Round %d\nYou win.\n",n); else if(error>=7) printf("Round %d\nYou lose.\n",n); else printf("Round %d\nYou chickened out.\n",n); } return 0; }
这个是我从网上找的能AC的
#include <stdio.h> #include <string.h> #define MAX 104 char answer[MAX]; char guess[MAX]; int alpha[MAX]; int main() { int a, i, j; #ifdef LOCAL freopen("data.in","r",stdin); #endif while (scanf("%d", &a) && a != -1) { getchar(); int flag, stroke = 0; memset(alpha, 1, MAX); gets(answer); gets(guess); printf("Round %d\n", a); for (i = 0; i < strlen(guess); i++) { flag = 0; if (alpha[guess[i] - 'a']) { for (j = 0; j < strlen(answer); j++) if (guess[i] == answer[j]) { answer[j] = '0'; flag = 1; } alpha[guess[i] - 'a'] = 0; if (!flag) stroke++; } if (stroke == 7) { printf("You lose.\n"); flag = 1; break; } flag = 1; for (j = 0; j < strlen(answer); j++) { if (answer[j] != '0') { flag = 0; break; } } if (flag) { printf("You win.\n");
break; } } if (!flag) printf("You chickened out.\n"); } return 0; }
虽然我没怎么看这个代码,但这个代码应该说和我的运行情况一样(详见后面)
这是网上找到的另一段代码
但是这段代码对错误我处理与上面那两个不一样,对于重复错误的会重复计算
比如输入
1
cheese
cqqqqqqqqqqqqqqqqqqqqhes
这段代码的结果和上面两个结果是不一样的
可很奇怪的是这段代码也能AC
#include<stdio.h> #include<string.h> const int MAXN=1000; int main() { int n,i,j,k; int ans,flag; int cas=1; int len,len_word,len_guess; char str1[MAXN],str2[MAXN]; while(scanf("%d",&n)) { ans=flag=len_word=len_guess=0; if(n==-1) break; scanf("%s",str1); scanf("%s",str2); for (i=0;str1[i];i++) { if(str1[i]!=1) { for (j=i+1;str1[j];j++) { if(str1[i]==str1[j]) str1[j]=1; } } } len=strlen(str1); printf("Round %d\n",n); for (i=0;str1[i];i++) { if(str1[i]!=1) len_word++; } for (i=0;str2[i];i++) { if(str2[i]==2) continue; for (j=0;str1[j];j++) { if(str2[i]==str1[j]) break; } if(j==len) ans++; else { len_guess++; for (j=i+1;str2[j];j++) { if(str2[i]==str2[j]) str2[j]=2; } } if(len_word==len_guess) { flag=1; printf("You win.\n"); break; } else if(ans==7) { flag=1; printf("You lose.\n"); break; } } if(!flag) printf("You chickened out.\n"); } return 0; }
UVaOJ
[解决办法] 引用: 引用:考虑某些边界条件,直接是W,还是其他错误?一直是WA while(scanf("%d",&n)&&n!=-1) { memset(sol,0,sizeof(sol)); memset(guess,0,sizeof(guess)); error=0; scanf("%s",sol); scanf("%s",guess); while(sol[l++]!='\0');//每次重新输入一组数据的时候要初始化l吧 l--; for(i=0;i<105&&guess[i]!=0;i++) { sign=1; for(j=0;j<105;j++)//这个地方优化下吧,不要每次都循环105次 { if(guess[i]==sol[j]) { sol[j]=0; sign=0; l--; } } if(sign) { for(j=i-1;j>=0;j--) if(guess[j]==guess[i]) sign=0; if(sign) error++; } if(l==0[解决办法] error>=7) break; } if(l==0) printf("Round %d\nYou win.\n",n); else if(error>=7) printf("Round %d\nYou lose.\n",n); else printf("Round %d\nYou chickened out.\n",n); }
代码中注释了两处,楼主改了然后试试,其他地方没发现什么错误
[解决办法] 你大while loop里面l没有清零。第二个数据开始的时候l可能是脏数据。