fork download
  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3. #define endl "\n"
  4.  
  5. int n , k , a[100];
  6. bool check = false;
  7. void khoiTao(){
  8. for(int i = 1 ; i <= k ; i++)
  9. a[i] = i ;
  10. }
  11.  
  12. void Sinh(){
  13. int i = k ;
  14. while(i >= 1 && a[i] == n - k + i){
  15. --i;
  16. }
  17. if(i == 0){
  18. check = true ; // danh dau day la bit cuoi cung
  19. }
  20. else{
  21. a[i] += 1 ; // tang a[i] len 1 don vi
  22. for(int j = i + 1 ; j <= k ; j++){
  23. a[j] = a[j - 1] + 1 ;
  24. }
  25. }
  26. }
  27.  
  28. int main(){
  29. ios::sync_with_stdio(false);
  30. cin.tie(nullptr);
  31. cout.tie(nullptr);
  32.  
  33. cin >> n >> k ;
  34. khoiTao();
  35. while(!check){
  36. for(int i = 1 ; i <= k ; i++){
  37. cout << a[i] ;
  38. }
  39. cout << endl;
  40. Sinh();
  41. }
  42.  
  43. }
Success #stdin #stdout 0.01s 5304KB
stdin
5 3
stdout
123
124
125
134
135
145
234
235
245
345