刚入坑,递归只会学校C语言那些,给个最菜的题解,还想了快两个小时,大佬勿喷
算法
递归,再细分不懂
时间复杂度
不会算(评论区求教)
C++ 代码
#include <cstdio>
#include <iostream>
using namespace std;
int output[25];
void f(int low, int high, int m,const int m_const)
{
int t;
for (t = low;t <= high+1-m;t++)
{
if (m > 0)
{
*(output + m-1) = t;
f(t + 1, high, m - 1, m_const);
}
if (m == 0)
{
for (int j = m_const - 1;j >= 0;j--)
printf("%d ",output[j]);
printf("\n");
break;
}
}
}
int main()
{
int n, m;
cin >> n >> m;
f(1, n, m, m);
return 0;
}