题意见紫书
代码很久以前写,不太简洁
因此再贴一个刘汝佳代码
#include<iostream> #include<cstring> using namespace std; const int N=26; int vis[10][N]; bool flag; bool sum(int *a) { for(int i=0;i<N;i++) if(a[i]) return false; return true; } int main() { int loop,ans=0,cnt; string str; while(cin>>loop&&loop>0) { flag=false; memset(vis,0,sizeof(vis)); cin>>str; for(int i=0;i<str.length();i++) { vis[0][str[i]-‘a‘]=1; vis[1][str[i]-‘a‘]=1; vis[2][str[i]-‘a‘]=1; } cin.clear(); cin>>str; cnt=0; for(int i=0;i<str.length();i++) { if(vis[1][str[i]-‘a‘]) vis[1][str[i]-‘a‘]=0; if(vis[2][str[i]-‘a‘]==0) cnt++,vis[2][str[i]-‘a‘]=1; if(cnt==7) { cout<<"Round "<<loop<<endl; cout<<"You lose."<<endl; flag=1;break; } if(sum(vis[1])) { cout<<"Round "<<loop<<endl; cout<<"You win."<<endl; flag=1;break; } } if(!flag) { cout<<"Round "<<loop<<endl; cout<<"You chickened out."<<endl; } } }
刘汝佳
// UVa489 Hangman Judge // Rujia Liu #include<stdio.h> #include<string.h> #define maxn 100 int left, chance; char s[maxn], s2[maxn]; int win, lose; void guess(char ch) { int bad = 1; for(int i = 0; i < strlen(s); i++) if(s[i] == ch) { left--; s[i] = ‘ ‘; bad = 0; } if(bad) --chance; if(!chance) lose = 1; if(!left) win = 1; } int main() { int rnd; while(scanf("%d%s%s", &rnd, s, s2) == 3 && rnd != -1) { printf("Round %d\n", rnd); win = lose = 0; left = strlen(s); chance = 7; for(int i = 0; i < strlen(s2); i++) { guess(s2[i]); if(win || lose) break; } if(win) printf("You win.\n"); else if(lose) printf("You lose.\n"); else printf("You chickened out.\n"); } return 0; }
原文地址:https://www.cnblogs.com/033000-/p/10037368.html
时间: 2024-10-14 01:15:58