ホームページ  >  記事  >  バックエンド開発  >  配列の二値性を計算するプログラム

配列の二値性を計算するプログラム

PHPz
PHPz転載
2023-08-29 20:53:06584ブラウズ

配列の二値性を計算するプログラム

配列の単調性は次のように定義されます:

配列要素に基づいて配列の単調性を見つけます:

Bitonicity = 0 , initially arr[0]
i from 0 to n
Bitonicity = Bitonicity+1 ; if arr[i] > arr[i-1]
Bitonicity = Bitonicity-1 ; if arr[i] < arr[i-1]
Bitonicity = Bitonicity ; if arr[i] = arr[i-1]

配列の bitonicity を見つけるコードでは、bitonicity と呼ばれる変数を使用します。この変数は、配列の現在の要素と前の要素の比較に基づいて変化します。上記のロジックは配列の階調性を更新し、最終的な階調性は配列の最後にあります。

#include <iostream>
using namespace std;
int main() {
   int arr[] = { 1, 2, 4, 5, 4, 3 };
   int n = sizeof(arr) / sizeof(arr[0]); int Bitonicity = 0;
   for (int i = 1; i < n; i++) {
      if (arr[i] > arr[i - 1])
         Bitonicity++;
      else if (arr[i] < arr[i - 1]) Bitonicity--;
   }
   cout << "Bitonicity = " << Bitonicity;
   return 0;
}

出力

Bitonicity = 1

以上が配列の二値性を計算するプログラムの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。