如何快速找出数字? [英] How to quickly figure out the numbers?

查看:90
本文介绍了如何快速找出数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我最近编写了一些关于快速猜测数字的代码工作的问题。该程序是这样的,由用户随机写入一个四位数(但计算机不知道多少),然后计算机向用户提问以确定数量,具体哪些内容不受限制。但是用户在计算机上的问题只能回答是或否。但计算机问题的数量尽可能少。所以,这个问题与公牛和奶牛有所不同。但我需要大约14次才能确定这个数字,而我找不到问题较少的解决方案,请帮帮我,不胜感激。



我尝试了什么:



为了解决这个问题,我尝试使用二进制搜索方法写一段代码。具体描述是,从第一位到第四位逐位确定相应的数字,每个数字在0-9用二进制搜索方法确定,所以平均14次确定四位数。

Hello everyone, I recently had some problems writing a code job about quick guessing numbers. The program is like this, by a user randomly wrote a four-digit (but the computer does not know how much), and then the computer to the user to ask questions to determine the number, specifically what content is not limited. But the user's question on the computer can only answer "yes" or "no". But the number of computer questions as little as possible. So, this question is different from "bulls and cows".But I need about 14 times to determine this number, and I can not find a solution with fewer questions, please help me, be grateful.

What I have tried:

In order to solve this problem, I try to use binary search method to write a piece of code. The specific description is, from the first to the fourth bit by bit to determine the corresponding number, each number in 0-9 using binary search method to determine, so an average of 14 times to determine the four digits.

推荐答案

你可以用13个步骤进行4位数,每次问题都是一样的:它是否小于当前值的一半?



初始范围:0000 - > 9999

是否小于5000?

如果是,则范围变为0000 - 4999.接下来的猜测是小于2500?

如果不是,则范围变为5000 - 9999.接下来的猜测是小于7500?

重复,并且在13个步骤中您将获得单个值。
You can do it in 13 steps for 4 digits, and the question is the same each time: Is it less than half the current value?
I.e.
Initial range: 0000 -> 9999
Is it less than 5000?
If yes, the range becomes 0000 - 4999. Next guess is "less than 2500?"
If no, the range becomes 5000 - 9999. Next guess is "less than 7500?"
Repeat, and in 13 steps you will have a single value.


Quote:

但我需要大约14次来确定这个数字,而我找不到问题较少的解决方案

But I need about 14 times to determine this number, and I can not find a solution with fewer questions



因为没有更快的解决方案。

二分法是解决问题的最快方法。

你有10000个可能的数字,2的权力是2 ^ 13 = 8192和2 ^ 14 = 16384.

这是你14来自的地方。


Because there is no faster solution.
Dichotomy is the fastest way to solve your problem.
Your have 10000 possible numbers, and powers of 2 are 2^13= 8192 and 2^14= 16384.
This is where your 14 comes from.


这篇关于如何快速找出数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆