22DH110739 - NguyenQuocDat

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Nguyễn Quốc Đạt - 22DH110739 - T22404

Backtracking
Bài tập 1:
using System;

namespace Backtracking
{
class Program
{
static int n, k;
static int[] x;

static void Try(int i)


{
if (i > k)
XuatNghiem();
else
{
for (int j = 1; j <= n; j++)
{
x[i] = j;
Try(i + 1);
}
}
}
static void XuatNghiem()
{
Console.Write("{");
for(int i = 1; i < k; i++)
Console.Write(x[i] + ", ");
Console.Write(x[k] + "}");
}
static void Main(string[] args)
{
string[] numbers =
Console.ReadLine().Split();
n = int.Parse(numbers[0]);
k = int.Parse(numbers[1]);
x = new int[k + 1];
Try(1);
}
}
}

You might also like