489 - Hangman Judge (UVA)

发布于:2023-09-15 ⋅ 阅读:(311) ⋅ 点赞:(0)

题目链接如下:

Online Judge

题目中有这么一句:“Each unique wrong guess only counts against the contestant once.” 修改后的代码如下(估计题目测试集有点问题?这一句感觉没测出来):

#include <cstdio>
#include <string.h>
#include <set>
const int maxx = 1000;

int rnd, tot;
char a[maxx], b[maxx];
bool flag;

int main(){
    while(scanf("%d", &rnd) == 1 && rnd != -1){
        printf("Round %d\n", rnd);
        scanf("%s %s", a, b);
        tot = 7;
        std::set<char> st, guessed;
        flag = false;
        for(int i = 0; i < strlen(a); ++i){
            st.insert(a[i]);
        }
        for(int i = 0; i < strlen(b); ++i){
            if(st.find(b[i]) != st.end()){
                st.erase(b[i]);
                if(st.empty()){
                    printf("You win.\n");
                    flag = true;
                    break;
                }
            } else if(guessed.find(b[i]) == guessed.end()){
                tot--;
                guessed.insert(b[i]);
                if(tot == 0){
                    printf("You lose.\n");
                    flag = true;
                    break;
                }
            }
        }
        if(flag){
            continue;
        }
        printf("You chickened out.\n");
    }
    return 0;
}

我原来的代码如下(能AC):

#include <cstdio>
#include <string.h>
#include <set>
const int maxx = 1000;

int rnd, tot;
char a[maxx], b[maxx];
bool flag;

int main(){
    while(scanf("%d", &rnd) == 1 && rnd != -1){
        printf("Round %d\n", rnd);
        scanf("%s %s", a, b);
        tot = 7;
        std::set<char> st;
        flag = false;
        for(int i = 0; i < strlen(a); ++i){
            st.insert(a[i]);
        }
        for(int i = 0; i < strlen(b); ++i){
            if(st.find(b[i]) != st.end()){
                st.erase(b[i]);
                if(st.empty()){
                    printf("You win.\n");
                    flag = true;
                    break;
                }
            } else{
                tot--;
                if(tot == 0){
                    printf("You lose.\n");
                    flag = true;
                    break;
                }
            }
        }
        if(flag){
            continue;
        }
        printf("You chickened out.\n");
    }
    return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到