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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. 《Cracking the Coding Interview》——第17章:普通题——题目14

    2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...

  10. 《Cracking the Coding Interview》——第17章:普通题——题目13

    2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...

随机推荐

  1. TP5.1:连接数据库(全局配置、动态配置、DSN配置)

    前提: (1)在app\index\controller文件下新建一个名为Connect.php的控制器文件 (2)建立一个名为user_curd数据库,里面有一张user表,表内容为: 通过全局配置 ...

  2. IOS 拦截所有push进来的子控制器

    /** * 能拦截所有push进来的子控制器 */ - (void)pushViewController:(UIViewController *)viewController animated:(BO ...

  3. [Pytorch] pytorch笔记 <一>

    pytorch笔记 - torchvision.utils.make_grid torchvision.utils.make_grid torchvision.utils.make_grid(tens ...

  4. test image

    Most of these images are in PBM or PGM format and compressed with GNU Zip and GNU TAR Note: These pa ...

  5. jq weui 图片浏览器Photo Browser 第一次点击任意图片总是显示第一张

    第一次做这个图片浏览器的时候遇到一个问题,如共有6张图片,每次进入页面时,第一次点击,无论去点击6张图片的哪一张初始化显示的都是第一张图片.后面的每次点击都没有问题的. for(let i = 0;i ...

  6. Java分享笔记:使用缓冲流复制文件

    [1] 程序设计 /*------------------------------- 1.缓冲流是一种处理流,用来加快节点流对文件操作的速度 2.BufferedInputStream:输入缓冲流 3 ...

  7. 轻量级自动化工具 pssh

    pssh应用场景 pssh是一个用python编写的可以并发在多台服务器上批量执行命令的工具,它支持文件并行复制,远程并行执行命令,其中文件并行复制是pssh的核心功能,也是同类工具中的一个亮点. 要 ...

  8. django+xadmin在线教育平台(十三)

    这个6-8对应对应6-11,6-12 拷入forgetpassword页面 书写处理忘记密码的view users/views.py # 用户忘记密码的处理view class ForgetPwdVi ...

  9. 7-1 python 操作redis

    1.安装并导入redis模块 # pip install redis 安装redis模块 import redis # 导入redis模块 2.连接一个或多个redis,指定数据库名,并指定返回字符串 ...

  10. 在github上查找star最多的项目

    如何在github上查找star最多的项目 在search中输入stars:>1 就可以查找所有有star的项目,然后右上角根据自己的需要筛选 当我输入stars:>10000的时候,就会 ...