C++でのN+1日目の雨の確率
0と1を含む配列が与えられます。ここで、0は雨が降っていないことを表し、1は雨の日を表します。タスクは、N+1日目の雨の確率を計算することです。
N + 1日目の雨の確率を計算するには、次の式を適用できます
セット内の合計雨日数/セット内の合計日数
入力
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
-
C++のチェス盤でのナイト確率
NxNチェス盤が1つあるとすると、騎士はr番目の行とc番目の列から開始し、正確にK回移動しようとします。ここでは、行と列に0のインデックスが付けられているため、左上の正方形は(0、0)であり、右下の正方形は(N-1、N-1)です。 騎士はセルから8つの異なるセルに移動できます。これは、この図に示されています- 騎士が移動するたびに、8つの可能な移動の1つをランダムに選択します。騎士は、正確にK移動するか、チェス盤から離れるまで移動を続けます。騎士が動きを止めた後もボードに留まる確率を見つける必要があります。 したがって、入力が3、2、0、0のような場合、出力は0.0625になります
-
C++で幸せな女性の日のプログラムを書く
世界中で10月7日に祝われる女性の日は、次のようにc++プログラミングコードに刻まれています。 例 #include <iostream> using namespace std; int main(){ // Initializing size of // design int n = 5; // Loop to print Circle // (Upper part of design) // Outer loop to &