《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 ...
随机推荐
- TP5.1:模板继承(重要知识点)
1.在app\index\controller文件夹新建一个名为Lyot(自定义)的控制器,在控制器中定义: 2.创建一个被继承的public(自定义)文件夹,里面有三个文件,分别是header.ht ...
- window下mycat要放在根目录下
原理文档没有跟我开玩笑呢? 建议放在盘符根目录下 ,无视的我,检查了多遍jdk环境,检查了多遍bat文件内容,仍然没有解决 找不到或无法加载主类 直到我乖乖的把目录放跟盘符才解决,心好累= =! 可能 ...
- 力不从心 Leetcode(ugly number heap) 263, 264,313
Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...
- 检测浏览器中是否有Flash插件
由于IE和非IE浏览器检测方式不同,所以代码如下 function hasPlugin(name){ debugger; name = name.toLowerCase(); for(var i=0; ...
- DedeCms中出现Safe Alert: Request Error step 1/2 的解决方法
dedecms安全警告:Safe Alert: Request Error step 2!不知道大家有没有发现这个现象.只从Dedecms官方公布了之前的版本有严重的漏洞以来,现在在仿站的时候都是采用 ...
- 2月4号学习的一个SSM整合项目,第一课
本文引自:https://github.com/Sunybyjava/seckill 原作者:sunybyjava@gmail.com seckill 一个整合SSM框架的高并发和商品秒杀项目,学习 ...
- MySQL 数据库和一些常用命令的使用
常用命令总结: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...
- 1.1 NBU基本概念
1.1 NBU基本概念 1) Policy(备份策略) 备份策略定义一台或几台服务器的备份方法.它包括哪些服务器需要备份.备份哪些目录或文件.在什么时间备份.采用什么方式进行备份等.配置NBU主要指定 ...
- Android Support v4,v7,v13的区别和应用场景
android-support-v4 是谷歌推出的兼容包,最低兼容Android1.6的系统,里面有类似ViewPager等控件.ViewPager在Android 1.6以下的版本是不自带的,所以要 ...
- frcnn_train_data_param的distort_param实现
frcnn_train_data_param frcnn_train_data_param { source: "./data/train_list.txt" root_folde ...