fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, k, X[1000];
  5.  
  6. void start() {
  7. for (int i = 0; i < n; i++) X[i] = 0;
  8. for (int i = n - k; i < n; i++) X[i] = 1;
  9. }
  10.  
  11. bool next_combination() {
  12. int i = n - 1;
  13. while (i >= 0 && X[i] == 1) {
  14. X[i] = 0;
  15. --i;
  16. }
  17. if (i < 0) return false; // Hết tổ hợp
  18.  
  19. X[i] = 1;
  20.  
  21. // Đưa tất cả số `1` về cuối
  22. int ones = 0;
  23. for (int j = i + 1; j < n; j++) {
  24. if (X[j] == 1) ones++;
  25. X[j] = 0;
  26. }
  27. for (int j = n - ones; j < n; j++) {
  28. X[j] = 1;
  29. }
  30.  
  31. return true;
  32. }
  33.  
  34. void generate() {
  35. do {
  36. for (int i = 0; i < n; i++) cout << X[i];
  37. cout << endl;
  38. } while (next_combination());
  39. }
  40.  
  41. int main() {
  42. int t;
  43. cin >> t;
  44. while (t--) {
  45. cin >> n >> k;
  46. start();
  47. generate();
  48. }
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0.01s 5292KB
stdin
2
4 2
3 2
stdout
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
011
100
101
110
111