17.5 The Came of Master Mind is played as follows: The computer has four slots, and each slot will contain a ball that is red (R), yellow (Y), green (C) or blue (B). For example, the computer might have RGGB (Slot # 1 is red, Slots #2 and #3 are green, Slot #4 is blue). You, the user, are trying to guess the solution. You might, for example, guess YRGB. When you guess the correct color for the correct slot, you get a "hit." If you guess a color that exists but is in the wrong slot, you get a "pseudo-hit." Note that a slot that is a hit can never count as a pseudo-hit. For example, if the actual solution is RGBYandyou guess GGRR, you have one hit and one pseudo-hit. Write a method that, given a guess and a solution, returns the number of hits and pseudo-hits.

LeetCode上的原题,讲解请参见我之前的博客Bulls and Cows

解法一:

string getHint(string answer, string guess) {
int m[] = {}, h = , p = ;
for (int i = ; i < answer.size(); ++i) {
if (answer[i] == guess[i]) ++h;
else ++m[answer[i]];
}
for (int i = ; i < answer.size(); ++i) {
if (answer[i] != guess[i] && m[guess[i]]) {
++p;
--m[guess[i]];
}
}
return to_string(h) + "H" + to_string(p) + "P";
}

解法二:

string getHint(string answer, string guess) {
int m[] = {}, h = , p = ;
for (int i = ; i < answer.size(); ++i) {
if (answer[i] == guess[i]) ++h;
else {
if (m[answer[i]]++ < ) ++p;
if (m[guess[i]]-- > ) ++p;
}
}
return to_string(h) + "H" + to_string(p) + "P";
}

CareerCup All in One 题目汇总

[CareerCup] 17.5 Game of Master Mind 猜字游戏的更多相关文章

  1. 猜字游戏java

    一.实践目的 1.掌握基本输入输出. 2.掌握方法定义与调用,理解参数传递方式. 3.掌握数组的声明.定义与初始化,数组的处理. 4.掌握数组作为方法参数和返回值. 二.实践要求 利用方法.数组.基本 ...

  2. Leetcode 299.猜字游戏

    猜字游戏 你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为"Bulls&q ...

  3. C - 一个C语言猜字游戏

    下面是一个简陋的猜字游戏,玩了一会儿,发现自己打不过自己写的游戏,除非赢了就跑,最高分没有过1000. 说明:srand(time(NULL))和rand(),srand,time和rand都是函数, ...

  4. 044 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 06 使用do-while循环实现猜字游戏

    044 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 06 使用do-while循环实现猜字游戏 本文知识点:do-while循环深入运用 案例练习 案例 ...

  5. 51 Nod 不一样的猜字游戏

    1536 不一样的猜数游戏  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 瓦斯亚和皮台亚在玩一个简单的游戏.瓦 ...

  6. 初识Java,猜字游戏

    import java.util.*; public class caizi{ public static void main(String[] args){ Scanner in=new Scann ...

  7. Java 7.35 游戏:猜字游戏(C++&Java)

    Ps: 有人可能好奇我为什么大费周章的去写主函数,而不直接把所有操作放在类中,Why?类就好比骨干(存放核心的数据和功能),最好提供接口, 避免了类中的输入输出,当然,这也不是绝对的. C++: #i ...

  8. python实现猜字游戏

    import randomx = random.randint(0,99)while(True): num = input("please input a number\n") i ...

  9. 猜数字游戏--基于python

    """题目:练习使用python写一个猜数字的游戏,数字范围0-100,每次猜错,需要给出缩小后的范围,每个人只有10次的猜测机会,猜测机会用完游戏结束!"&q ...

随机推荐

  1. 第十二篇:SOUI的utilities模块为什么要用DLL编译?

    SOUI相对于DuiEngine一个重要的变化就是很多模块变成了一个单独的DLL. 然后很多情况下用户可能希望整个产品就是一个EXE,原来DuiEngine提供了LIB编译模式,此时链接LIB模式的D ...

  2. UI中 frame 与 transform的用法与总结

    在iOS中,我们是不可以直接访问控件中frame的结构体的成员的,因此我们需要分三步来改变一个UI控件的位置,大小 一.frame用法 frame的结构体类型为: struct CGRect { CG ...

  3. javascript 时间倒计时

    新加入一个项目的集中开发,遇到一个需要倒计时的需求,经过测试,有以下几种方案,分享出来: 方案一: 页面Html: <span style="font-size:18px;" ...

  4. kafka producer源码

    producer接口: /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor l ...

  5. 【POI】使用POI处理xlsx的cell中的超链接 和 插入图片 和 设置打印区域

    使用POI对xlsx中插入超链接和 插入图片 package com.it.poiTest; import java.awt.image.BufferedImage; import java.io.B ...

  6. poj2533 LIS

    题目链接: http://poj.org/problem?id=2533 题意:第一个数n,接下来n个数,>  ....求最长上升子序列. 这道题有两种解法,第一种是通解,也适用于别的LIS. ...

  7. 模数转换(A/D)和数模转换(D/A) 图示

    (从书中截图) 在时间域和频率域中示意图: 1.A/D转换 2.D/A转换

  8. 运行re-sign.jar重签名工具报错ERROR:Cannot run program "D:\sdk\tools\zipalign

    今天在使用这个拖拽到具,把apk文件拖到re-sign.jar运行打开的界面,报错如下: ERROR:Cannot run program "E:\Android sdk\sdk\tools ...

  9. PyCharm默认快捷键

    转自:http://www.2cto.com/os/201410/341542.html 1.编辑(Editing)Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + ...

  10. HTML5拖放事件(Drag-and-Drop,DnD)

    拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 拖放是在“拖放源(drag source)”和“拖放目标(drop target ...