0
点赞
收藏
分享

微信扫一扫

Chapter 4

我是小瘦子哟 2022-03-11 阅读 28

Example

4-1

4-2

#include <stdio.h>
#include <string.h>

#define maxn 100
int left, chance; // 还需要猜left个位置,错chance次后就会输
char s[maxn], s2[maxn]; // 答案是s,玩家猜测的字符序列为s2
int win, loss; // win = 1 表示赢,loss = 1 表示输

void guess (char ch) {
    int bad = 1; // 注意这个bad的用法,很聪明!
    for (int i = 0; i < strlen(s); i++) {
        if (s[i] == ch) {
            left--;
            s[i] = ' ';
            bad = 0;
        }
    }
    if (bad)
        --chance;
    if (!chance)
        loss = 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 = loss = 0;
        left = strlen(s);
        chance = 7;
        
//      printf("s = %s\n s2 = %s\n", s, s2);
        
        for (int i = 0; i < strlen(s2); i++) {
            guess(s2[i]);
            if (win || loss) 
                break;
        }
        
        // 根据结果输出
        if (win) printf("You win.\n");
        else if (loss) printf("You loss.\n");
        else printf("You chickened out.\n");
    }
    return 0;
}
举报

相关推荐

0 条评论