Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

นับลำดับเพิ่มขึ้นทั้งหมดใน C++


ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมที่หาจำนวนลำดับที่เพิ่มขึ้น

สำหรับสิ่งนี้ เราจะได้รับอาร์เรย์ที่มีตัวเลข 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