《Cracking the Coding Interview》——第17章:普通题——题目5
2014-04-28 22:44
题目:猜数字游戏。四个数字,每个都是0~9之间。你每猜一次,我都告诉你,有多少个位置和数字都对(全对),有多少个位置错数字对(半对)。比如“6309”,你猜“3701”,就有1全对,1半对。
解法:依照题意写就可以了。
代码:
// 17.5 I am the Master Mind. Guess the number.
// When you guessed the right number at the right position, you got a hit.
// When you guessed the right number at the wrong position, you got a pseudo-hit.
#include <cstdio>
using namespace std; void check(int solution[], int guess[], int &hit, int &pseudo_hit)
{
static int c[];
static int mark[]; int i;
for (i = ; i < ; ++i) {
c[i] = ;
mark[i] = ;
} hit = pseudo_hit = ;
for (i = ; i < ; ++i) {
if (solution[i] == guess[i]) {
// hit
mark[i] = ;
++hit;
} else {
++c[solution[i]];
}
}
for (i = ; i < ; ++i) {
if (mark[i]) {
continue;
}
// pseudo_hit
if (c[guess[i]] > ) {
mark[i] = ;
--c[guess[i]];
++pseudo_hit;
}
}
} int main()
{
int solution[];
int guess[];
int hit, pseudo_hit;
int i; while (scanf("%d", &solution[]) == ) {
for (i = ; i < ; ++i) {
scanf("%d", &solution[i]);
}
for (i = ; i < ; ++i) {
scanf("%d", &guess[i]);
}
check(solution, guess, hit, pseudo_hit);
printf("%d hit(s), %d pseudo-hit(s).\n", hit, pseudo_hit);
} return ;
}
《Cracking the Coding Interview》——第17章:普通题——题目5的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目14
2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
随机推荐
- ubuntu16.4安装 VirtualBox
1) 从oracle官网下载virtual box安装包 2) 安装支持包 sudo apt-get install libqt5x11extras5 libsdl1.2debian 3) sudo ...
- UE4工具
COMMON CONTAINERS TARRAY (Engine\Source\Runtime\Core\Public\Containers\Array.h) TSET (Engine\Source\ ...
- Jerry Wang诚邀广大SAP同仁免费加入我的知识星球,共同探讨SAP技术问题
大家知道Jerry Wang有一个微信公众号"汪子熙",2017年12月27日,Jerry的这个公众号发布了第一篇文章.到今天2018年10月底为止,正好十个月. 在这10个月的时 ...
- 在idea中配置jetty
第一步:在pom.xml中配置jetty插件 <plugins> <!-- jetty插件 --> <plugin> <groupId>org.mort ...
- rnn,cnn
http://nikhilbuduma.com/2015/01/11/a-deep-dive-into-recurrent-neural-networks/ 按照这里的介绍,目前比较火的cnn是fee ...
- jQuery性能优化和技巧
jQuery性能优化 ①使用最新版本的jQuery类库 ②使用合适的选择器 ③缓存对象 ④循环时的DOM操作 ⑤数组方式使用jQuery对象 ⑥事件代理 ⑦将你的代码转化成jQuery插件 ⑧使用jo ...
- css 伪类选择器制作登录框表单
使用伪类选择器 制作鼠标悬停时文本框出现橙色虚线边框 制作鼠标激活时出现背景颜色淡橙色 使用css制作文本框圆角矩形效果,制作文本框背景图片,及背景不重复效果 <!DOCTYPE html> ...
- 使用Git操作码云
一.安装并配置 .安装git 下载地址: 官方网站:https://git-for-windows.github.io/ 国内镜像:https://pan.baidu.com/s/1kU5OCOB#l ...
- LeetCode46. Permutations
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- Scott Young-《如何高效学习》
1.如果你只用一种方式了解某样事物,那么你就没有真正了解它.事情真正含义的秘密取决于我们如何将其与我们所了解的其他事情相联系.很好联系的内容可使你将想法融于脑中,从各种角度看问题,直至你找到合适自己的 ...