[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 ...
随机推荐
- DrawerLayout
一.概述 DrawerLayout是官方提供的侧滑菜单,相比SliddingMenu,它更加轻量级.默认情况下,DrawerLayout可以设置左侧或者右侧滑出菜单.如下, x ...
- pythonpython-eggs异常解决方法
: UserWarning: /home/server/.python-eggs is writable by group/others and vulnerable to attack when u ...
- Python入门神图
国外某小哥制作的Python入门神图
- hdu 2191 多重背包
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- Eclipse如何替换android应用图标
打开你的项目 我们看到项目里有 res这个文件夹里有以下文件夹. drawable-hdpi -------高分辨率 drawable-ldpi -------中分辨率 drawab ...
- css随记01编辑技巧,背景与边框
代码优化 一个按钮的例子,使其值同比例变化; button{ color: white; background: #58a linear-gradient(#77a0bb, #58a); paddin ...
- Swift3.0语言教程删除字符与处理字符编码
Swift3.0语言教程删除字符与处理字符编码 Swift3.0语言教程删除字符 Swift3.0语言教程删除字符与处理字符编码,在字符串中,如果开发者有不需要使用的字符,就可以将这些字符删除.在NS ...
- angular的post请求,SpringMVC后台接收不到参数值的解决方案
http://www.ithao123.cn/content-6567265.html
- Tomcat 配置 Context
在 conf/Catalina/localhost/ 下添加 xml配置文件,文件名和站点名一致. 配置文件示例 <?xml version='1.0' encoding='utf-8'?> ...
- FString的相关文档,另外还有4种LOG的方法
https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/StringHandling/FString/index ...