紫書——Hangman Judge UVA - 489
阿新 • • 發佈:2018-11-09
題解:
這條題目比較容易,水題,但是相信有人會嘗試debug裡面的測試點之後會發現不同,但是提交之後缺ac了,因為(第一個)測試點是猜一個猜過的字母算錯,但是錯一個以錯的字母不算錯,而題目只要求第一個
下面附上題目要求的解法:
#include <bits/stdc++.h> using namespace std; char aa[50],bb[50]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n; string sa,sb; while(scanf("%d",&n) == 1){ if(n == -1) break; scanf("%s%s",aa,bb); int lena = strlen(aa); int lenb = strlen(bb); int wor = 0,num = lena; for(int i = 0; i < lenb; i++){ int ok = 1; for(int j = 0; j < lena; j++){ if(bb[i] == aa[j]){ aa[j] = ' '; num--; ok = 0; } } if(ok) wor++; if(wor == 7) break; } printf("Round %d\n",n); if(num == 0) printf("You win.\n"); else if(wor < 7 && num) printf("You chickened out.\n"); else printf("You lose.\n"); } return 0; }
下面的為debug1測試點的解法
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<algorithm> using namespace std; const int maxn = 1000; char str1[maxn], str2[maxn], c; int size1, size2, yes, no, k; bool win, lose; int isFind(char str[], int size, char c) { for (int i = 0; i < size; i++) { if (str[i] == c) return 1; } return 0; } int main() { //freopen("input.txt","r",stdin); while (scanf("%d",&k) == 1) { if (k < 0)break; size1 = 0; size2 = 0; yes = 0; no = 0; win = 0; lose = 0; getchar(); memset(str1, 0, sizeof(str1)); memset(str2, 0, sizeof(str2)); while ((c = getchar()) != '\n') { if (!isFind(str1,size1,c)) { str1[size1++] = c; } } while ((c = getchar()) != '\n') { if (isFind(str1,size1,c) && !isFind(str2,size2,c)) yes++; //注意細節~ else if (!isFind(str1,size1,c) && !isFind(str2,size2,c))no++; if ((yes < size1) && (no == 7)) { lose = 1; } else if ((yes == size1) && (no < 7)) { win = 1; } str2[size2++] = c; } if (win) { printf("Round %d\nYou win.\n",k); } if (lose) { printf("Round %d\nYou lose.\n",k); } if (yes < size1 && no < 7) { printf("Round %d\nYou chickened out.\n",k); } } //freopen("CON","r",stdin); //system("pause"); return 0; }