ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อค้นหาจำนวนลำดับที่เพิ่มขึ้น
สำหรับสิ่งนี้ เราจะได้รับอาร์เรย์ที่มีตัวเลข 0 ถึง 9 หน้าที่ของเราคือนับลำดับทั้งหมดที่มีอยู่ในอาร์เรย์เพื่อให้องค์ประกอบถัดไปมีค่ามากกว่าองค์ประกอบก่อนหน้า
ตัวอย่าง
#include<bits/stdc++.h>
using namespace std;
//counting the possible subsequences
int count_sequence(int arr[], int n){
int count[10] = {0};
//scanning each digit
for (int i=0; i<n; i++){
for (int j=arr[i]-1; j>=0; j--)
count[arr[i]] += count[j];
count[arr[i]]++;
}
//getting all the possible subsequences
int result = 0;
for (int i=0; i<10; i++)
result += count[i];
return result;
}
int main(){
int arr[] = {3, 2, 4, 5, 4};
int n = sizeof(arr)/sizeof(arr[0]);
cout << count_sequence(arr,n);
return 0;
} ผลลัพธ์
14