在C ++中,第N + 1天下雨的概率
给定一个包含0和1的数组,其中0表示没有下雨,而1表示下雨天。任务是计算第N+1天下雨的概率。
要计算第N+1天下雨的概率,我们可以应用公式
集合中的雨天总数/
输入值
arr[] = {1, 0, 0, 0, 1 }输出结果
第n+1天下雨的概率 : 0.4
说明
total number of rainy and non-rainy days are: 5 Total number of rainy days represented by 1 are: 2 第n+1天下雨的概率 is: 2 / 5 = 0.4
输入值
arr[] = {0, 0, 1, 0}输出结果
第n+1天下雨的概率 : 0.25
说明
total number of rainy and non-rainy days are: 4 Total number of rainy days represented by 1 are: 1 第n+1天下雨的概率 is: 1 / 4 = 0.25
给定程序中使用的方法如下
输入数组的元素
输入1代表雨天
输入0代表非雨天
通过应用上面给出的公式计算概率
打印结果
算法
Start
Step 1→ Declare Function to find 第n+1天下雨的概率
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;
//第n+1天下雨的概率
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<<"第n+1天下雨的概率 : "<<probab_rain(arr, size);
return 0;
}输出结果
如果运行上面的代码,它将生成以下输出-
第n+1天下雨的概率 : 0.4
热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短