Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void getPossibleComb(int level, int n, int k, string path, vector<vector<int> > &combinations){
- if(level > 9){
- if(n == 0 && k == 0){
- vector<int> integerPath;
- for(char c : path){
- integerPath.push_back(c - '0');
- }
- combinations.push_back(integerPath);
- }
- return;
- }
- // use the number
- if(k > 0 && n - level >= 0){
- getPossibleComb(level + 1, n - level, k - 1, path + to_string(level), combinations);
- }
- //don't use the number
- getPossibleComb(level + 1, n, k, path, combinations);
- }
- int main() {
- // your code goes here
- int n, k;
- cin >> n >> k;
- vector<vector<int> > combinations;
- getPossibleComb(1, n, k, "", combinations);
- for(int i = 0; i < combinations.size(); i++){
- for(int j = 0; j < combinations[i].size(); j++){
- cout << combinations[i][j] << " ";
- }
- cout << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement