回溯
#include <iostream>
#include <vector>
using namespace::std;
using std::cout;
using std::cin;
void jump(int n, int k, vector<int>& path, vector<vector<int>>& result)
{
if (n == 0)
{
result.push_back(path);
return;
}
for (int i = 1; i <= k && i <= n; i++)
{
path.push_back(i);
jump(n - i, k, path, result);
path.pop_back();
}
}
vector<vector<int>> exerciseWays(int n, int k)
{
vector<vector<int>> result;
vector<int> path;
jump(n, k, path, result);
return result;
}
int main()
{
int n,k;
cin>> n >> k;
vector<vector<int>> result = exerciseWays(n, k);
for (const auto& path : result)
{
for (int i = 0; i < path.size(); i++)
{
std::cout << path[i];
if (i != path.size() - 1) {
std::cout << " ";
}
}
std::cout << "\n";
}
return 0;
}