听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • UVA

    • UVA340 - Master-Mind Hints
    • UVA401 - Palindromes
    • UVA409 - Excuses, Excuses!
    • UVA414 - Machined Surfaces
    • UVA424 - Integer Inquiry
    • UVA445 - Marvelous Mazes
    • UVA457 - Linear Cellular Automata
    • UVA458 - The Decoder
    • UVA465 - Overflow
    • UVA488 - Triangle Wave
    • UVA489 - Hangman Judge
      • 问题描述
      • 思路
      • 代码
    • UVA490 - Rotating Sentences
    • UVA494 - Kindergarten Counting Game
    • UVA537 - Artificial Intelligence?
    • UVA644 - Immediate Decodability
    • UVA694 - The Collatz Sequence
    • UVA748 - Exponentiation
    • UVA10010 - Where's Waldorf?
    • UVA10055 - Hashmat the Brave Warrior
    • UVA10071 - Back to High School Physics
    • UVA10106 - Product
    • UVA10115 - Automatic Editing
    • UVA10250 - The Other Two Trees
    • UVA10300 - Ecological Premium
    • UVA10361 - Automatic Poetry
    • UVA10420 - List of Conquests
    • UVA10494 - If We Were a Child Again
    • UVA10815 - Andy's First Dictionary
    • UVA10878 - Decode the tape
  • LeetCode

  • ACM
  • UVA
zfprotectors
2022-05-18
目录

UVA489 - Hangman Judge

# 问题描述

 输入一个要猜测的答案字符串s,猜的字符串s2,判断猜测的字符错误次数为7次以内,7次以内并且全部猜中算赢,如果没猜中,则算放弃,如果错7次或以上则判定为输。

# 思路

进行比较字符时,将比较成功的字符改成空格,极大简化了步骤。

# 代码

紫书(《算法竞赛入门经典(第二版)》)中的代码简洁明了。

#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 100
using namespace std;
int Left, chance;       // 还需猜Left个位置,错chance次后就会输
char s[maxn],s2[maxn];  // 答案是字符串s,玩家猜出的字母序列为s2
int win,lose;           // win=1为赢,lose=1为输
void guess(char ch)
{
    int bad=1;
    for(int i=0;i<strlen(s);i++)
        if(s[i]==ch){Left--;s[i]=' ';bad=0;}
    if(bad)
        chance--;
    if(!chance)
        lose=1;
    if(!Left)
        win=1;
}
//判断成功后,更改字符为空格然后重新进行判断
int main()
{
    int rnd;
    while(cin>>rnd&&rnd!=-1)
    {
        scanf("%s%s",s,s2);
        printf("Round %d\n",rnd);
        win=lose=0;
        Left=strlen(s);
        chance=7;
        for(int i=0;i<strlen(s2);i++)
        {
            guess(s2[i]);
            if(win||lose)
                break;
        }
        if(win)
            printf("You win.\n");
        else if(lose)
            printf("You lose.\n");
        else
            printf("You chickened out.\n");

    }
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
编辑 (opens new window)
#ACM#C++#UVA
上次更新: 2022/05/18, 19:03:08
UVA488 - Triangle Wave
UVA490 - Rotating Sentences

← UVA488 - Triangle Wave UVA490 - Rotating Sentences→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式