[CareerCup] 17.5 Game of Master Mind 猜字游戏
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] 17.5 Game of Master Mind 猜字游戏的更多相关文章
- 猜字游戏java
一.实践目的 1.掌握基本输入输出. 2.掌握方法定义与调用,理解参数传递方式. 3.掌握数组的声明.定义与初始化,数组的处理. 4.掌握数组作为方法参数和返回值. 二.实践要求 利用方法.数组.基本 ...
- Leetcode 299.猜字游戏
猜字游戏 你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为"Bulls&q ...
- C - 一个C语言猜字游戏
下面是一个简陋的猜字游戏,玩了一会儿,发现自己打不过自己写的游戏,除非赢了就跑,最高分没有过1000. 说明:srand(time(NULL))和rand(),srand,time和rand都是函数, ...
- 044 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 06 使用do-while循环实现猜字游戏
044 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 06 使用do-while循环实现猜字游戏 本文知识点:do-while循环深入运用 案例练习 案例 ...
- 51 Nod 不一样的猜字游戏
1536 不一样的猜数游戏 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 瓦斯亚和皮台亚在玩一个简单的游戏.瓦 ...
- 初识Java,猜字游戏
import java.util.*; public class caizi{ public static void main(String[] args){ Scanner in=new Scann ...
- Java 7.35 游戏:猜字游戏(C++&Java)
Ps: 有人可能好奇我为什么大费周章的去写主函数,而不直接把所有操作放在类中,Why?类就好比骨干(存放核心的数据和功能),最好提供接口, 避免了类中的输入输出,当然,这也不是绝对的. C++: #i ...
- python实现猜字游戏
import randomx = random.randint(0,99)while(True): num = input("please input a number\n") i ...
- 猜数字游戏--基于python
"""题目:练习使用python写一个猜数字的游戏,数字范围0-100,每次猜错,需要给出缩小后的范围,每个人只有10次的猜测机会,猜测机会用完游戏结束!"&q ...
随机推荐
- 第十七篇:使用窗口的cache属性加速SOUI的渲染
内容渲染速度是决定一个UI成败的关键.无论UI做得多华丽,没有速度都没有意义. 在MFC,WTL等开发框架下,每个控件都是一个窗口,窗口只需要画前景,背景.因为窗口之间的内容不需要做混合,一个子窗口的 ...
- 直接拿来用!最火的Android开源项目(二)(转)
GitHub上的开源项目不胜枚举,通过这些项目,也能让开发者在应用开发过程中事半功倍,作为开发者的你,在用这些开源项目吗?今天我们将介绍另外20个在GitHub上备受欢迎的Android开源项目,你准 ...
- 1.ARC和非ARC文件共存
1.ARC和非ARC文件共存 项目->Build Parses->对应的类 1.1.新项目兼容老的非ARC:-fno-objc-arc 1.2.老项目兼容ARC:-fobjc-arc
- 【bzoj2440】【bzoj3994】莫比乌斯反演学习
哇..原来莫比乌斯代码这么短..顿时感觉逼格-- 写了这道题以后,才稍稍对莫比乌斯函数理解了一些 定理:和是定义在非负整数集合上的两个函数,并且满足条件,那么我们得到结论 在上面的公式中有一个函数,它 ...
- SlidesJS的使用
项目中对slideshow要求要有触屏滑动换图功能,就想到了SlidesJS这个Jquery插件 例排,先把静态html写好 <div id="cm_slides"> ...
- OA
OA(政府) http://zw.wuzhitong.com/oa_wuzhitong/index.asp http://www.wuzhitong.com/Information_ZW.html O ...
- SQL: See the TSQL underneath the sp_execute calls
When use SQL Server Profiler, on the Events Selection tab, check Show all events; Find the Store Pro ...
- Visual Studio蛋疼问题解决
监视变量显示未定义标识符: VS2012在编译的时候采用了较快的编译模式,所以有些变量就显示未定义了. 解决方案: 项目->属性->c/c++->优化->改为禁用/OD ...
- BZOJ3161 : 孤舟蓑笠翁
显然求出每个点到所有关键点的最短路和次短路即可,答案就是每个关键点的次短路. 设$f[i][j][0]$表示左手在$i$,右手在$j$的解,$f[i][j][1]$表示左手在$i$,右手在$j$,且左 ...
- Twitter Storm源代码分析之ZooKeeper中的目录结构
徐明明博客:Twitter Storm源代码分析之ZooKeeper中的目录结构 我们知道Twitter Storm的所有的状态信息都是保存在Zookeeper里面,nimbus通过在zookeepe ...