给定一个数组A[],数组的大小为N,另一个密钥为K。任务是找到密钥K出现在数组中的概率。 例如:
null
Input : N = 6 A[] = { 4, 7, 2, 0, 8, 7, 5 } K = 3Output :0Since value of k = 3 is not present in array,hence the probability of 0.Input :N = 10 A[] = { 2, 3, 5, 1, 9, 8, 0, 7, 6, 5 } K = 5Output :0.2
可使用以下公式计算出发生故障的概率:
Probability = total number of K present / size of array.
首先,计算K的数量,然后概率将是K的数量除以N,即计数/N。 以下是上述方法的实施情况:
C++
// C++ code to find the probability of // search key K present in array #include <bits/stdc++.h> using namespace std; // Function to find the probability float kPresentProbability( int a[], int n, int k) { float count = 0; for ( int i = 0; i < n; i++) if (a[i] == k) count++; // find probability return count / n; } // Driver Code int main() { int A[] = { 4, 7, 2, 0, 8, 7, 5 }; int K = 3; int N = sizeof (A) / sizeof (A[0]); cout << kPresentProbability(A, N, K); return 0; } |
Python3
# Python3 code to find the # probability of search key # K present in 1D-array (list). # Function to find the probability def kPresentProbability(a, n, k) : count = a.count(k) # find probability upto # 2 decimal places return round (count / n , 2 ) # Driver Code if __name__ = = "__main__" : A = [ 4 , 7 , 2 , 0 , 8 , 7 , 5 ] K = 2 N = len (A) print (kPresentProbability( A, N, K)) # This code is contributed # by AnkitRai1 |
JAVA
// Java code to find the probability // of search key K present in array class GFG { // Function to find the probability static float kPresentProbability( int a[], int n, int k) { float count = 0 ; for ( int i = 0 ; i < n; i++) if (a[i] == k) count++; // find probability return count/ n; } // Driver Code public static void main(String[] args) { int A[] = { 4 , 7 , 2 , 0 , 8 , 7 , 5 }; int K = 2 ; int N = A.length; double n = kPresentProbability(A, N, K); double p = ( double )Math.round(n * 100 ) / 100 ; System.out.println(p); } } // This code is contributed // by ChitraNayal |
C#
// C# code to find the probability // of search key K present in array using System; class GFG { // Function to find the probability static float kPresentProbability( int [] a, int n, int k) { float count = 0; for ( int i = 0; i < n; i++) if (a[i] == k) count++; // find probability return count/ n; } // Driver Code public static void Main() { int [] A = { 4, 7, 2, 0, 8, 7, 5 }; int K = 2; int N = A.Length; double n = kPresentProbability(A, N, K); double p = ( double )Math.Round(n * 100) / 100; Console.Write(p); } } // This code is contributed // by ChitraNayal |
PHP
<?php // PHP code to find the probability // of search key K present in array // Function to find the probability function kPresentProbability(& $a , $n , $k ) { $count = 0; for ( $i = 0; $i < $n ; $i ++) if ( $a [ $i ] == $k ) $count ++; // find probability return $count / $n ; } // Driver Code $A = array ( 4, 7, 2, 0, 8, 7, 5 ); $K = 2; $N = sizeof( $A ); echo round (kPresentProbability( $A , $N , $K ), 2); // This code is contributed // by ChitraNayal ?> |
Javascript
<script> // JavaScript code to find the probability of // search key K present in array // Function to find the probability function kPresentProbability(a,n,k) { let count = 0; for (let i = 0; i < n; i++) if (a[i] == k) count+=1; // find probability return count / n; } // Driver Code let A = [ 4, 7, 2, 0, 8, 7, 5 ]; let K = 3; let N = A.length; document.write(kPresentProbability(A, N,K)); // This code contributed by Rajput-Ji </script> |
输出
0
时间复杂度:O(N)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END