Untitled
CPP
views 19
,
size 1237 b
``````// Trieu Than @Pro - let's grow together!

#include<iostream>
using namespace std;

int a[50], n, k;

void nhap(){
cout<<"\nNhap n, k: ";
cin>> n >> k;
}

/*	cac buoc thuc hien thuat toan:
B1: khoi tao cau hinh dau tien.
B2: Lap neu chua tim thay to hop cuoi cung:
B2.0: xuat to hop hien tai ra.
B2.1: tim to hop ke tiep:
B2.1.1: i = k;
bat dau tu cuoi to hop hien tai,
tìm a[i] sao cho a[i]  != n+i-k;
B2.1.2: cap nhat a[i]: a[i] = a[i] + 1;
B2.1.3: neu i> 0 thi cap nhat not cac
phan tu sau chi so i cua to hop.
nguoc lai thi ket thuc.
*/

/*	vi du: ban dau: 1 2 3-> 1 2 4
to hop ke tiep: 1 2 4 -> 1 2 5
tu to hop 1 2 5-> thay a[3] = 5 - > vi a[2] # 4, ta update a[2]
=> 1 3 4;
*/

void sinh( int &ok ){
int i = k;
while( i > 0 && a[i] == n+i-k ) i--;
a[i]++;
if( i > 0 ){
for( int j = i+1; j<= n; j++ )
a[j] = a[i] + j-i;
}
else ok = 0; // da tim dc to hop cuoi cung
}

void xuat( ){
for( int i = 1; i<= k; i++  )
cout<< a[i] << " ";
cout<< endl;
}

void next( int ok ){
for( int i = 1; i<= n; i++  ) a[i] = i;
while( ok ){
xuat();
sinh( ok );
}
}

int main(){
int ok = 1;
nhap();
next( ok );
return 0;
}``````