紫书——Hangman Judge UVA - 489

版权声明: https://blog.csdn.net/a673953508/article/details/81071215

题解:

这条题目比较容易,水题,但是相信有人会尝试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;  
}  

猜你喜欢

转载自blog.csdn.net/a673953508/article/details/81071215