用 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP