原文:

给定表示一个范围的两个整数 lr ,任务是当在范围 l 到 r 中选择一个随机数时,找到得到一个数的。 举例:

输入: l = 6,r = 20 输出: 0.133333 解释: 范围内的完美方块【6,20】= { 9,16} = > 2 完美方块 范围内的总数【6,20】= 15 概率= 2 / 15 = 0.133333 输入: l = 16

方法:这个问题的关键观察是从 0 到一个数范围内的完美平方的计数可以用给定的公式计算:

//0 到 n 范围内的完美正方形的计数为 完美正方形的计数=地板(sqrt(n))

类似地,在给定范围内的完美正方形的计数可以借助于上述公式计算如下:

完美正方形的计数[l,r] =地板(sqrt(r))–天花板(sqrt(l)) 1 范围内的总数= r–l 1

以下是上述方法的实现:

c

// c   implementation to find the
// probability of getting a
// perfect square number
#include 
using namespace std;
// function to return the probability
// of getting a perfect square
// number in a range
float findprob(int l, int r)
{
    // count of perfect squares
    float countofps = floor(sqrt(r)) - ceil(sqrt(l))   1;
    // total numbers in range l to r
    float total = r - l   1;
    // calculating probability
    float prob = (float)countofps / (float)total;
    return prob;
}
// driver code
int main()
{
    int l = 16, r = 25;
    cout << findprob(l, r);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation to find the
// probability of getting a
// perfect square number
class gfg{
// function to return the probability
// of getting a perfect square
// number in a range
static float findprob(int l, int r)
{
    // count of perfect squares
    float countofps = (float) (math.floor(math.sqrt(r)) -
                               math.ceil(math.sqrt(l))   1);
    // total numbers in range l to r
    float total = r - l   1;
    // calculating probability
    float prob = (float)countofps / (float)total;
    return prob;
}
// driver code
public static void main(string[] args)
{
    int l = 16, r = 25;
    system.out.print(findprob(l, r));
}
}
// this code is contributed by amit katiyar

python 3

# python3 implementation to find 
# the probability of getting a
# perfect square number
import math
# function to return the probability
# of getting a perfect square
# number in a range
def findprob(l, r):
    # count of perfect squares
    countofps = (math.floor(math.sqrt(r)) -
                  math.ceil(math.sqrt(l))   1)
    # total numbers in range l to r
    total = r - l   1
    # calculating probability
    prob = countofps / total
    return prob
# driver code
if __name__=='__main__':
    l = 16
    r = 25
    print(findprob(l, r))
# this code is contributed by rutvik_56   

c

// c# implementation to find the probability
// of getting a perfect square number
using system;
class gfg{
// function to return the probability
// of getting a perfect square
// number in a range
static float findprob(int l, int r)
{
    // count of perfect squares
    float countofps = (float)(math.floor(math.sqrt(r)) -
                            math.ceiling(math.sqrt(l))   1);
    // total numbers in range l to r
    float total = r - l   1;
    // calculating probability
    float prob = (float)countofps / (float)total;
    return prob;
}
// driver code
public static void main(string[] args)
{
    int l = 16, r = 25;
    console.write(findprob(l, r));
}
}
// this code is contributed by amit katiyar

java 描述语言


output: 

0.2

时间复杂度: o(1)