Title Link: https://uva.onlinejudge.org/external/4/489.pdf
This question is like a topological sort of topic before, the idea is similar.
Program modularity:
Each time a letter is judged, Lose,win is determined to exit directly.
Tips:
You can use the array guess[] to record whether each letter has been accessed. If it had been visited, counts++ was a mistake.
Here Ru Jia's little trick is, every time the same word assigned value for ", record the length of str1, check out str1 on win. Otherwise you are chickened out.
#include <stdio.h>#include<string.h>intwin, lose;intLeft ;intCounts =0;Charstr1[ the],str2[ the];voidGuessCharch) { BOOLFlag =false; for(intI=0; I<strlen (str1); i++) { if(ch==Str1[i]) {Str1[i]=' '; Flag=true; Left--; } } if(!flag) counts++; if(counts>6) Lose =1; if(left==0) win =1;}intMain () {Freopen ("Input.txt","R", stdin); intcases; while(SCANF ("%d",&cases)) {Counts=0; if(cases==-1) Break; printf ("Round%d\n", cases); scanf ("%s%s", STR1,STR2); Win= lose =0; Left=strlen (STR1); for(intI=0; I<strlen (str2); i++) {guess (str2[i]); if(win| |lose) Break; } if(Win) printf ("You win.\n"); Else if(lose) printf ("You lose.\n"); Elseprintf"You chickened out.\n"); } return 0;}
UVa 489, Purple book P79, hangman games