[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 ...
随机推荐
- (C#基础) byte[] 之初始化, 赋值,转换。(转)
byte[] 之初始化赋值 用for loop 赋值当然是最基本的方法,不过在C#里面还有其他的便捷方法. 1. 创建一个长度为10的byte数组,并且其中每个byte的值为0. byte[] myB ...
- [Java] arraycopy 数组复制(转)
public class ArraycopyTest { public static void main(String[] args) { //静态初始化两个长度不同的 ...
- AndroidStudio中创建Assets文件
- 【SQL 触发器】
一.MySQL上触发器的使用 示例: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW t ...
- 安卓图表引擎AChartEngine(三) - 示例源码折线图、饼图和柱状图
折线图: package org.achartengine.chartdemo.demo.chart; import java.util.ArrayList; import java.util.Lis ...
- 通过jquery.transit.min.js插件,实现图片的移动
首先给出插件:jquery.transit.min.js (function(t,e){if(typeof define==="function"&&define. ...
- nefu 197 KMP
Description 在信息检索系统中一个很重要的环节就是关键字符串的查找,从而很多对自己有用的信息.给你一个很长的一段文字, 和一个关键信息字符串,现在要你判断这段文字里面是否有关键字符串. In ...
- Uva 129 Krypton Factor
0.这道题的输出 处理起来挺麻烦的 以后类似的可以借鉴一下 ;i<cur;i++) { && i%==) printf("\n%c",a[i]); & ...
- blade用法
一.blade条件判断,foreach循环写法 @if(isset($fileInfo) && !empty($fileInfo)) @foreach($fileInfo as $k) ...
- Android自动化测试-Robotium(一)简介
一.Robotium原理 Robotium是一款Android自动化测试框架,主要针对Android平台的应用进行黑盒自动化测试,它提供了模拟各种手势操作(点击.长按.滑动等).查找和断言机制的API ...