#include <iostream> #include <vector> void combinations(std::vector<int>& chosen, int start, int n, int k) { if (k == 0) { for (int num : chosen) { std::cout << num << " "; } std::cout << std::endl; return; } for (int i = start; i <= n; ++i) { chosen.push_back(i); combinations(chosen, i+1, n, k-1); chosen.pop_back(); } } int main() { int n, k; std::cin >> n >> k; std::vector<int> chosen; combinations(chosen, 1, n, k); return 0; }
Python:
def combinations(chosen, start, n, k): if k == 0: print(chosen) return for i in range(start, n+1): chosen.append(i) combinations(chosen, i+1, n, k-1) chosen.pop() n = int(input()) k = int(input()) chosen = [] combinations(chosen, 1, n, k)
Pascal:
program Combinations; var chosen: array of Integer; procedure Combinations(start, n, k: Integer); var i: Integer; begin if k = 0 then begin for i := 0 to Length(chosen) - 1 do Write(chosen[i], ' '); WriteLn; Exit; end; for i := start to n do begin SetLength(chosen, Length(chosen) + 1); chosen[Length(chosen) - 1] := i; Combinations(i+1, n, k-1); SetLength(chosen, Length(chosen) - 1); end; end; var n, k: Integer; begin ReadLn(n); ReadLn(k); Combinations(1, n, k); end.
олютно все рабочие языки
#include <iostream>C++:
#include <vector>
void combinations(std::vector<int>& chosen, int start, int n, int k) {
if (k == 0) {
for (int num : chosen) {
std::cout << num << " ";
}
std::cout << std::endl;
return;
}
for (int i = start; i <= n; ++i) {
chosen.push_back(i);
combinations(chosen, i+1, n, k-1);
chosen.pop_back();
}
}
int main() {
int n, k;
std::cin >> n >> k;
std::vector<int> chosen;
combinations(chosen, 1, n, k);
return 0;
}
Python:
def combinations(chosen, start, n, k):if k == 0:
print(chosen)
return
for i in range(start, n+1):
chosen.append(i)
combinations(chosen, i+1, n, k-1)
chosen.pop()
n = int(input())
k = int(input())
chosen = []
combinations(chosen, 1, n, k)
Pascal:
program Combinations;var
chosen: array of Integer;
procedure Combinations(start, n, k: Integer);
var
i: Integer;
begin
if k = 0 then
begin
for i := 0 to Length(chosen) - 1 do
Write(chosen[i], ' ');
WriteLn;
Exit;
end;
for i := start to n do
begin
SetLength(chosen, Length(chosen) + 1);
chosen[Length(chosen) - 1] := i;
Combinations(i+1, n, k-1);
SetLength(chosen, Length(chosen) - 1);
end;
end;
var
n, k: Integer;
begin
ReadLn(n);
ReadLn(k);
Combinations(1, n, k);
end.