zoukankan      html  css  js  c++  java
  • UVa 489 HangmanJudge --- 水题

      UVa 489

      题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错,

           你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜过的单词也算出错,

           给定计算机的单词以及猜测序列,判断玩家赢了(You win)、输了(You lose)、放弃了(You chickened out)

    /* UVa 489 HangmanJudge --- 水题 */
    #include <cstdio>
    #include <cstring>
    
    int kase;
    char a[105], b[105];
    int len1, len2;
    int left, chance;
    int win, lose;
    
    /* 猜字母ch */
    void guess(char ch){
        bool bad = 1; //默认猜错
        for (int i = 0; i < len1; ++i){
            //若相等则猜对
            if (ch == a[i]){
                --left;
                bad = 0;    //更改猜错标记
                a[i] = ' ';    //更改此处字符,下次再猜算猜错
            }
        }//for(i)
        if (bad){
            --chance;
        }
    
        if (!left){
            win = 1;
        }
        else if (!chance){
            lose = 1;
        }
    
    }
    
    int main()
    {
    #ifdef _LOCAL
        freopen("D:\input.txt", "r", stdin);
    #endif
    
        while (scanf("%d", &kase) == 1 && kase != -1){
            scanf("%s%s", a, b);    
            
            win = lose = 0;    //还没赢也还没输
            chance = 7;    //剩余7次机会
            left = len1 = strlen(a);    //剩余left个字母还没猜对
            len2 = strlen(b);
            for (int i = 0; i < len2; ++i){
                guess(b[i]);    //猜字母
                if (win || lose){
                    break;
                }
            }
            printf("Round %d
    ", kase);
            if (win){
                printf("You win.
    ");
            }
            else if (lose){
                printf("You lose.
    ");
            }
            else{
                printf("You chickened out.
    ");
            }
    
        }//while(scanf)
    
        return 0;
    }
    View Code
  • 相关阅读:
    云计算架构
    Java多线程中static变量的使用
    Spring单例与线程安全小结
    sparkSQL实战详解
    sparkSQL整体实现框架
    spark架构
    如何快速定位出一个IP地址的归属地?——二分查找变体
    如何在 1000 万个整数中快速查找某个整数?——二分查找
    语言库中常用的排序算法qsort()底层结构
    链表常考笔试面试题(常备)
  • 原文地址:https://www.cnblogs.com/tommychok/p/5357621.html
Copyright © 2011-2022 走看看