0과 1이 포함된 배열이 주어지며 0은 비가 오지 않음을 나타내고 1은 비오는 날을 나타냅니다. 과제는 N+1일에 비가 올 확률을 계산하는 것입니다.
N+1일에 비가 올 확률을 계산하기 위해 다음 공식을 적용할 수 있습니다.
세트의 총 비오는 날 수 / a의 총 일수
입력
arr[] = {1, 0, 0, 0, 1 } 출력
probability of rain on n+1th day : 0.4
설명
total number of rainy and non-rainy days are: 5 Total number of rainy days represented by 1 are: 2 Probability of rain on N+1th day is: 2 / 5 = 0.4
입력
arr[] = {0, 0, 1, 0} 출력
probability of rain on n+1th day : 0.25
설명
total number of rainy and non-rainy days are: 4 Total number of rainy days represented by 1 are: 1 Probability of rain on N+1th day is: 1 / 4 = 0.25
주어진 프로그램에서 사용되는 접근 방식은 다음과 같습니다.
-
배열의 요소 입력
-
비오는 날을 나타내는 입력 1
-
비가 오지 않는 날을 나타내는 0 입력
-
위에 주어진 공식을 적용하여 확률 계산
-
결과 인쇄
알고리즘
Start
Step 1→ Declare Function to find probability of rain on n+1th day
float probab_rain(int arr[], int size)
declare float count = 0, a
Loop For int i = 0 and i < size and i++
IF (arr[i] == 1)
Set count++
End
End
Set a = count / size
return a
step 2→ In main()
Declare int arr[] = {1, 0, 0, 0, 1 }
Declare int size = sizeof(arr) / sizeof(arr[0])
Call probab_rain(arr, size)
Stop 예시
#include <bits/stdc++.h>
using namespace std;
//probability of rain on n+1th day
float probab_rain(int arr[], int size){
float count = 0, a;
for (int i = 0; i < size; i++){
if (arr[i] == 1)
count++;
}
a = count / size;
return a;
}
int main(){
int arr[] = {1, 0, 0, 0, 1 };
int size = sizeof(arr) / sizeof(arr[0]);
cout<<"probability of rain on n+1th day : "<<probab_rain(arr, size);
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
probability of rain on n+1th day : 0.4