|
发表于 2021-7-14 23:35:57
|
显示全部楼层
本楼为最佳答案
改了一个变量的名字,原来是 n_size,现在改成 size
之前是有 buff_size 和 n_size,后来发现 buff_size 并不需要就删除了,但是 n_size 就没有必要指明是 n 的 size 了
现在改回来
- #include <stdio.h>
- static void combination_sub(const int n[], size_t size, size_t m, void (*const visit)(const int n[], size_t size), int buff[], size_t buff_index) {
- if(m == 0) {visit(buff, buff_index); return;}
- for(size_t i = 0; i + m <= size; ++i) {
- buff[buff_index] = n[i];
- combination_sub(n + 1 + i, size - 1 - i, m - 1, visit, buff, buff_index + 1);
- }
- }
- void combination(const int n[], size_t size, size_t m, void (*const visit)(const int n[], size_t size)) {
- if(m > 1024) return;
- int buff[1024]; combination_sub(n, size, m, visit, buff, 0);
- }
- void visit(const int n[], size_t size) {
- for(size_t i = 0; i < size; ++i) printf("%d ", n[i]);
- printf("\n");
- }
- int main(void) {
- int n[20]; for(size_t i = 0; i < 20; ++i) n[i] = i + 1;
- combination(n, 20, 5, visit);
- return 0;
- }
复制代码 |
|