Implement Rand10() Using Rand7()

2024-09-04 14:32
文章标签 using implement rand7 rand10

本文主要是介绍Implement Rand10() Using Rand7(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10.

Do NOT use system's Math.random().

Example 1:

Input: 1
Output: [7]

思路:数学题,1~7 可以转换成 0 ~ 48 ,然后0~40 可以取模10,得到0~9+1 得到1~10;

取到灰色的格子,就不要了,取前40个,因为前面40个的出现的次数都是4次,是相等的; 

/*** The rand7() API is already defined in the parent class SolBase.* public int rand7();* @return a random integer in the range 1 to 7*/
class Solution extends SolBase {public int rand10() {int rand40 = Integer.MAX_VALUE;while(rand40 >= 40) {rand40 = (rand7() - 1) * 7 + (rand7() - 1);}return rand40 % 10 + 1;}
}

这篇关于Implement Rand10() Using Rand7()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136243

相关文章

【智力题】由Rand5()生成Rand7()

题目 给定生成1到5的随机数Rand5(),如何得到生成1到7的随机数函数Rand7()? 要从一个生成1到5的随机数的函数 Rand5() 中生成一个1到7的随机数的函数 Rand7(),我们可以利用均匀分布的性质和一些数学技巧。 方法概述 生成更大的范围:首先,我们可以通过调用 Rand5() 两次来生成一个更大的范围。具体来说,我们可以将两个 Rand5() 的结果组合成一个数字,形

Implement HashMap in Java

Implement Hashmap in Java.  原理: Hashmap就是array of linkedlist,就是利用hash算法,算index之后,然后冲突了,就看后面有没有 Key Value pair 相等,如果相同,则覆盖,如果没有,则加在bucket的beginning。 源代码: http://developer.classpath.org/doc/java/util/H

Build Min Heap Using Array

Build min-heap using Array. 思路1:首先明白heap的底层implementation就是array,从0开始的parent和left,right的关系为, 如果现在的node index为i,那么parent index就是 (i-1)/2;   left  为2*i+1, right为 2*i+2;          ( i-1 ) / 2

Implement Set using Array.

参考链接:http://faculty.washington.edu/moishe/javademos/ch03%20Code/jss2/ArraySet.java 被Pivotal的面试官给问到了,trick的地方在于remove的那一块,要把最后的元素跟自己remove的元素进行互换,然后count--;还有,自动扩容那块,构造函数需要两个,一个默认的,一个是可以限定side的。然后扩容的时

C# 使用中点查找矩形的角(Find Corners of Rectangle using mid points)

考虑一个矩形 ABCD,我们给出了边 AD 和 BC 中点(分别为 p 和 q)的坐标以及它们的长度 L(AD = BC = L)。现在给定参数,我们需要打印 4 个点 A、B、C 和 D 的坐标。 例子:  输入:p = (1, 0)         q = (1, 2)         L = 2 输出:(0,0),(0,2),(2,2),(2,0) 解释: 打

[转载] Java中extend 与 implement 区别

参考博客原址 https://blog.csdn.net/wm5920/article/details/9982057 简单说:  extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承,JAVA中不支持多重继承,但是可以用接口来实现,这样就要用到implements,继承只能继承一个类,但implements可以实现多个接口,用逗号分开就行了 比如

【C++】using简写

using TransformStamped = geometry_msgs::msg::TransformStamped;   作用:定义了一个类型别名 TransformStamped,简化了 geometry_msgs::msg::TransformStamped 的书写。目的:提高代码可读性和简洁性。 2. 在 Tf2ListenerModule 中的应用 class Tf2L

Working with excel files using Pandas

https://www.geeksforgeeks.org/working-with-excel-files-using-pandas/

using showdown js with openAi streaming response

题意:"使用 Showdown.js 处理 OpenAI 流式响应" 问题背景: I tried using showdownjs to translate streamed markdown from OpenAi to HTML "我尝试使用 Showdown.js 将来自 OpenAI 的流式 Markdown 转换为 HTML" I changed the code g

命名空间using namespace std

文章目录 为什么要使用命名空间如何自主定义命名空间命名空间的使用方法 为什么要使用命名空间  命名空间的存在是为了提高代码效率,有效的管理编写代码过程中常用的一些常见关键字 #include <vector>#include <iostream>using namespace std;void main() {cout << "hello,world" << endl;}