- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 C++ 和一般编程还很陌生。我决定要制作一个“猜数字”游戏,但我想看看计算机平均需要猜多少次才能猜出 1 到 10,000,000 之间的数字。
我能想到的找到“ secret ”号码的最简单方法是1. 将范围除以二(除数),这就是猜测。
一个。如果猜测大于“ secret ”数字,则猜测 1 成为范围的新最大值,然后我返回步骤 1。b.如果猜测值低于“ secret ”数字,则猜测值+1 成为范围的新最小值,然后我返回步骤 1。
重复此过程直到找到数字。根据我的经验,计算机需要 22 次猜测才能猜出“ secret ”数字。
为了好玩,我想看看如果我改变除数会发生什么。对于 2 到 10 的除数范围内的 1,000,000 次迭代尝试猜测 1 到 10,000,000 之间的数字的结果,我实际上有点惊讶。
Average with divisor 2 is 22.3195
Average with divisor 3 is 20.5549
Average with divisor 4 is 20.9087
Average with divisor 5 is 22.0998
Average with divisor 6 is 23.1571
Average with divisor 7 is 25.5232
Average with divisor 8 is 25.927
Average with divisor 9 is 27.1941
Average with divisor 10 is 28.0839
我很想知道为什么当使用除数 3、4 和 5 时,计算机平均能够使用更少的猜测来找到“ secret ”数字。
我的代码如下。
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
using namespace std;
int half_search(int a, int b, int n, int m)
{
int aMax = b;
int aMin = a;
int divisor = m;
int result;
static int counter = 0;
double guess = ((static_cast<double>(b) - a) / divisor) + aMin;
if(guess - static_cast<int>(guess) >= 0.5)
guess = ceil(guess);
if(guess < n)
{
aMin = guess + 1;
counter++;
half_search(aMin, aMax, n, divisor);
}
else if(guess > n)
{
aMax = guess - 1;
counter++;
half_search(aMin, aMax, n, divisor);
}
else
{
counter++;
result = counter;
counter = 0;
return result;
}
}
int main()
{
const int MIN = 1;
const int MAX = 10000000;
int k = 0;
int j = 2; //represents lowest divisor
int l = 10; //represent highest divisor
int iterations = 100000;
double stepSum = 0;
vector<int> myVector(iterations);
srand(1);
while(j <=10)
{
while(k < iterations)
{
int n = rand() % MAX + 1;
myVector[k] = half_search(MIN, MAX, n, j);
stepSum += myVector[k];
k++;
}
cout << "Average with divisor " << j << " is " << stepSum / iterations << endl;
j++;
k = 0;
stepSum = 0;
}
return 0;
}
最佳答案
在某些编译器上(例如 Visual Studio 2013)int n = rand() % MAX + 1;
将只提供 1 到 32768 之间的数字,因为 RAND_MAX
可以是低至 32767。
如果您的随机数非常小,这将偏向于较大的除数。
考虑使用 < random > 而不是在 C++11 中。像这样的东西:
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dist(1, MAX);
//...
int n = dist(mt);
关于c++ - 猜数字 : How many guesses for computer to get correct number?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26909167/
从 angular 5.1 更新到 6.1 后,我开始从我的代码中收到一些错误,如下所示: Error: ngc compilation failed: components/forms/utils.
我正在学习 Typescript 并尝试了解类型和接口(interface)的最佳实践。我正在玩一个使用 GPS 坐标的示例,想知道一种方法是否比另一种更好。 let gps1 : number[];
type padding = [number, number, number, number] interface IPaddingProps { defaultValue?: padding
这两种格式在内存中保存结果的顺序上有什么区别吗? number = number + 10; number += 10; 我记得一种格式会立即保存结果,因此下一行代码可以使用新值,而对于另一种格式,
在 Python 匹配模式中,如何匹配像 1 这样的文字数字在按数字反向引用后 \1 ? 我尝试了 \g用于此目的的替换模式中可用的语法,但它在我的匹配模式中不起作用。 我有一个更大的问题,我想使用一
我的源文件here包含 HTML 代码,我想将电话号码更改为可在我的应用程序中单击。我正在寻找一个正则表达式来转换字符串 >numbernumber(\d+)$1numbernumber<",我们在S
我们有一个包含 2 个字段和一个按钮的表单。我们想要点击按钮来输出位于 int A 和 int B 之间的随机整数(比如 3、5 或 33)? (不需要使用 jQuery 或类似的东西) 最佳答案 你
我收到以下类型错误(TypeScript - 3.7.5)。 error TS2345: Argument of type '(priority1: number, priority2: number
只想创建简单的填充器以在其他功能中使用它: function fillLine(row, column, length, bgcolor) { var sheet = SpreadsheetApp
我有一个问题。当我保存程序输出的 *.txt 时,我得到以下信息:0.021111111111111112a118d0 以及更多的东西。 问题是: 这个数字中的“d0”和“a”是什么意思? 我不知道“
首先:抱歉标题太长了,但我发现很难用一句话来解释这个问题;)。是的,我也四处搜索(这里和谷歌),但找不到合适的答案。 所以,问题是这样的: 数字 1-15 将像这样放在金字塔中(由数组表示):
我想从字符串中提取血压。数据可能如下所示: text <- c("at 10.00 seated 132/69", "99/49", "176/109", "10.12 I 128/51, II 1
当尝试执行一个简单的 bash 脚本以将前面带有 0 的数字递增 1 时,原始数字被错误地解释。 #!/bin/bash number=0026 echo $number echo $((number
我有一个类型为 [number, number] 的字段,TypeScript 编译器(strict 设置为 true)出现问题,提示初始值值(value)。我尝试了以下方法: public shee
你能帮我表达数组吗:["232","2323","233"] 我试试这个:/^\[("\d{1,7}")|(,"\d{1,7}")\]$/ 但是这个表达式不能正常工作。 我使用 ruby(rail
这个问题在这里已经有了答案: meaning of (number) & (-number) (4 个回答) 关闭6年前. 例如: int get(int i) { int res = 0;
我正在考虑使用 Berkeley DB作为高度并发的移动应用程序后端的一部分。对于我的应用程序,使用 Queue对于他们的记录级别锁定将是理想的。但是,如标题中所述,我需要查询和更新概念建模的数据,如
我正在尝试解决涉及重复数字的特定 JavaScript 练习,为此我需要将重复数字处理到大量小数位。 目前我正在使用: function divide(numerator, denominator){
我有这个数组类型: interface Details { Name: string; URL: string; Year: number; } interface AppState {
我们正在使用 Spring 3.x.x 和 Quartz 2.x.x 实现 Web 应用程序。 Web 服务器是 Tomcat 7.x.x。我们有 3 台服务器。 Quartz 是集群式的,因此所有这
我是一名优秀的程序员,十分优秀!