You are on page 1of 6

Chương 3

1.1
#include <bits/stdc++.h>
using namespace std;
int n, a[100], ok;
void kt ()
{
for (int i = 1; i <= n; i++)
{
a[i] = 0;
}
}
void sinh()
{
int i = n;
while (i >= 1 && a[i] == 1)
{
i -= 1;
}
if (i == 0)
ok = 0;
else
a[i] = 1;
}
void in()
{
for (int i = 1; i <= n; i++)
cout << a[i];
cout << endl;
sinh();
}
int main()
{
int t;
cin >> t;
while (t--)
{
ok = 1;
cin >> n;
kt();
while (ok)
{
in();
}
}
}
1.2
#include <bits/stdc++.h>
using namespace std;
int n, a[100], k, ok;
void kt()
{
for (int i = 1; i <= k; i++)
{
a[i] = i;
}
}
void sinh()
{
int i = k;
while (i >= 1 && a[i] == n - k + i)
{
i -= 1;
}
if (i == 0)
ok = 0;
else
{
a[i] += 1;
for (int j = i + 1; j <= k; j++)
{
a[j] = a[j - 1] + 1;
}
}
}
void in()
{
for (int i = 1; i <= k; i++)
cout << a[i];
cout << endl;
sinh();
}
int main()
{
int t;
cin >> t;
while (t--)
{
ok = 1;
cin >> n >> k;
kt();
while (ok)
{
in();
}
}
}
1.3
#include <bits/stdc++.h>
using namespace std;
int n, a[100], ok;
void kt()
{
for (int i = 1; i <= n; i++)
{
a[i] = i;
}
}
void sinh()
{
int i = n - 1;
while (i >= 1 && a[i] > a[i + 1])
{
i -= 1;
}
if (i == 0)
ok = 0;
else
{
int j = n;
while (a[i] > a[j])
j -= 1;
swap(a[i], a[j]);
reverse(a + i + 1, a + n + 1);
}
}
void in()
{
for (int i = 1; i <= n; i++)
cout << a[i];
cout << endl;
sinh();
}
int main()
{
int t;
cin >> t;
while (t--)
{
ok = 1;
cin >> n;
kt();
while (ok)
{
in();
}
}
}
2.1
#include <bits/stdc++.h>
using namespace std;
int a[100];
int n;
void inkq()
{
for (int i = 1; i <= n; i++)
{
cout << a[i];
}
cout << " ";
}
void Try(int i)
{
for (int j = 0; j <= 1; j++)
{
a[i] = j;
if (i == n)
inkq();
else
Try(i + 1);
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n;
Try(1);
cout << endl;
}
}
2.2
#include <bits/stdc++.h>
using namespace std;
int a[100];
int n, k;
void input()
{
for (int i = 1; i <= k; i++)
{
a[i] = i;
}
}
void inkq()
{
for (int i = 1; i <= k; i++)
{
cout << a[i];
}
cout << ' ';
}
void Try(int i)
{
for (int j = a[i - 1] + 1; j <= n - k + i; j++)
{
a[i] = j;
if (i == k)
inkq();
else
Try(i + 1);
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> k;
input();
Try(1);
cout << endl;
}
}
2.3
#include <bits/stdc++.h>
using namespace std;
int a[100];
int n;
bool used[100];
void inkq()
{
for (int i = 1; i <= n; i++)
{
cout << a[i];
}
cout << " ";
}
void Try(int i)
{
for (int j = 1; j <= n; j++)
{
if (!used[j])
{
a[i] = j;
used[j] = true;
if (i == n)
inkq();
else
Try(i + 1);
used[j] = false;
}
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
memset(used, false, sizeof(used));
cin >> n;
Try(1);
cout << endl;
}
}
Bài 2:
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[100];
void Try(int i)
{
for (int j = 0; j <= 1; j++)
{
a[i] = j;
if (i == n)
{
int dem = 0;
for (int l = 1; l <= n; l++)
{
if (a[l] == 1)
{
dem++;
}
}
if (dem == k)
{
for (int l = 1; l <= n; l++)
cout << a[l];
cout << endl;
}
}
else
Try(1 + 1);
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> k;
Try(1);
}
}

You might also like