编写一个C++函数,该函数以一个整数向量作为输入,并返回一个向量向量,其中输出中的每个向量表示输入向量的子集。但是,函数应该排除包含任何连续整数的子集。
时间: 2024-05-15 09:16:50 浏览: 82
```c
#include <stdio.h>
#include <stdlib.h>
int check_subsequence(int* arr, int len){
int i;
for(i=1; i<len; i++){
if(arr[i] - arr[i-1] == 1){
return 1;
}
}
return 0;
}
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
int i, j, k, count = 0, **res = NULL;
*returnSize = 1<<numsSize;
*returnColumnSizes = (int*)malloc(sizeof(int)*(*returnSize));
for(i=0; i<*returnSize; i++){
(*returnColumnSizes)[i] = 0;
for(j=0; j<numsSize; j++){
if(i&(1<<j)){
(*returnColumnSizes)[i]++;
}
}
}
res = (int**)malloc(sizeof(int*)*(*returnSize));
for(i=0; i<*returnSize; i++){
res[i] = (int*)malloc(sizeof(int)*(*returnColumnSizes)[i]);
k = 0;
for(j=0; j<numsSize; j++){
if(i&(1<<j)){
res[i][k++] = nums[j];
}
}
if(!check_subsequence(res[i], (*returnColumnSizes)[i])){
count++;
}
}
int** ans = (int**)malloc(sizeof(int*)*count);
count = 0;
for(i=0; i<*returnSize; i++){
if(!check_subsequence(res[i], (*returnColumnSizes)[i])){
ans[count++] = res[i];
}
}
return ans;
}
int main(){
int nums[] = {1,2,3,4};
int numsSize = sizeof(nums) / sizeof(nums[0]);
int returnSize, *returnColumnSizes, **ans;
ans = subsets(nums, numsSize, &returnSize, &returnColumnSizes);
for(int i=0; i<returnSize; i++){
for(int j=0; j<returnColumnSizes[i]; j++){
printf("%d ", ans[i][j]);
}
printf("\n");
}
return 0;
}
```
阅读全文