You are on page 1of 20

BÀI TẬP BUỔI 2

1.Viết giao diện tháp HANOI


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ThapHaNoi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)


{

private void btnSB_Click(object sender, EventArgs e)


{
int n;
if (int.TryParse(txtN.Text, out n))
{
int sobuoc = (int)(Math.Pow(2, n) - 1);
txtSB.Text = sobuoc.ToString();
}

private void txtN_TextChanged(object sender, EventArgs e)


{
int n;
if (int.TryParse(txtN.Text, out n))
{
while (n <= 0)
{
MessageBox.Show("Xin moi nhap lai so duong: ");
txtN.Clear();
txtN.Focus();
// trả về khỏi vòng lặp
return;

}
}

}
private void txtSB_TextChanged(object sender, EventArgs e)
{

private void btnKQ_Click(object sender, EventArgs e)


{
int n;
if (int.TryParse(txtN.Text, out n))
{
// Xóa danh sách trong list box
lstTH.Items.Clear();

// Gọi phương thức đệ quy để giải quyết bài toán


ThapHaNoi(n, "A", "B", "C");
}
}
private void ThapHaNoi(int n, string A, string B, string C)
{
if (n == 1)
{
// Di chuyển đĩa trên cùng từ 'A' sang 'C'
lstTH.Items.Add($"Di chuyển đĩa {n} từ {A} sang {C}");
return;
}

// Di chuyển n-1 đĩa trên cùng từ 'A' sang 'B' sử dụng 'C' làm cột trung gian
ThapHaNoi(n - 1, A, C, B);

// Di chuyển đĩa còn lại từ 'A' sang 'C'


lstTH.Items.Add($"Di chuyển đĩa {n} từ {A} sang {C}");

// Di chuyển n-1 đĩa trên cùng từ 'B' sang 'C' sử dụng 'A' làm cột trung gian
ThapHaNoi(n - 1, B, A, C);
}

private void btnX_Click(object sender, EventArgs e)


{

lstTH.Items.Clear();
txtSB.Clear();
txtN.Clear();
txtN.Focus();
}
}
}
2.Giao diện tính giai thừa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Giaodientinhgiaithua
{
public partial class TINHGIAITHUA : Form
{
public TINHGIAITHUA()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void label1_Click(object sender, EventArgs e)


{
}

private void textBox2_TextChanged(object sender, EventArgs e)


{

private void textBox1_TextChanged(object sender, EventArgs e)


{
int n;
if (int.TryParse(txtN.Text, out n))
{
while(n<0)
{
MessageBox.Show("Mời Bạn Nhập Lại!");
txtN.Clear();
return;
}

txtN.Text = n.ToString();
}
}

private void btnKQ_Click(object sender, EventArgs e)


{
int n;
if (int.TryParse(txtN.Text, out n))
{
if (n >= 0)
{
int TinhGiaiThua = GiaiThua(n);
txtKQ.Text = TinhGiaiThua.ToString();
}
}

}
//Tính Giai Thua
private int GiaiThua(int n)
{
if(n==0)
{
return 1;

}
else
{
return n * GiaiThua(n - 1);
}
}

private void btnX_Click(object sender, EventArgs e)


{
// Xóa
txtN.Clear();
txtKQ.Clear();
txtN.Focus();
}
}
}

Fi
bonacci dùng đệ qui
using System;

class Program
{
static void Main(string[] args)
{
//khai báo biến n sử dụng cho toàn hàm main
int n;
//Nhập đến khi nào n>0
do
{
Console.Write("Enter value n = ");
n = int.Parse(Console.ReadLine());
} while( n <0);

//gán giá trị cho result rồi xuất ra kết quả


int result = Fibonacci(n);
Console.WriteLine($"Fibonacci({n}) = {result}");
}
//Hàm tính Fibonacci bằng đệ qui
private static int Fibonacci(int n)
{
if (n == 0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
}

Fi
bonacci không dùng đệ qui
using System;

class Program
{
static void Main(string[] args)
{
int n;
do
{
Console.Write("Enter Value n = ");
n = int.Parse(Console.ReadLine());
}while(n < 0);

int f0 = 0;
int f1 = 1;
int fib=0;

if (n == 0)
{
fib = f0;
}
else if (n == 1)
{
fib = f1;
}
else
{
for (int i = 2; i <= n; i++)
{
fib = f0 + f1;
f0 = f1;
f1 = fib;
}
}

Console.WriteLine($"Gia tri Fibonacci tai vi tri thu ({n}) bang {fib}");


}
}

using System;
class Program
{
static void Main(string[] args)
{
int n;
do
{
Console.Write("Enter Value n = ");
n = int.Parse(Console.ReadLine());
} while (n < 0);
Console.WriteLine($"Gia tri Neper thu {n} la {Neper(n)} ");

}
/*
Trong vòng lặp, chúng ta tính số hạng kế tiếp ai+1 dựa trên số hạng hiện tại ai bằng cách chia ai cho(k+1). Sau đó, ta
cộng số hạng hiện tại vào tổng sum.
Sau khi vòng lặp kết thúc, phương thức trả về giá trị sum.*/
public static double Neper(int n)
{
//Biến sum được khởi tạo bằng 0, đại diện cho tổng a0 + a1 + ... + an.
double sum = 0;
// Biến a được khởi tạo bằng 1, đại diện cho số hạng đầu tiên a0 = 1.
double a = 1;
//Với N=2 .khi K=0 , sum=1,a=1 ; K=1 , sum = 1 a=1/2 ,Khi K=2, sum=2.5,a=1/3
for (int k = 0; k <= n; k++)
{
sum += a;
a /= (k + 1);
}
return sum;
}
}

using System;

public class Program


{
//m=372, n=84, GCĐeQui(84,36) ; m=84 ,n=36;GCĐeQui(36,12);m=36, n=12, GCĐeQui(12,0);
static int GCDDeQui(int m, int n)
{
if (n == 0)
return m;
else
return GCDDeQui(n, m % n);
}

static int GCD_KDeQui(int m, int n)


{
while (n != 0)
{
int temp = n;
n = m % n;
m = temp;
}
return m;
}

static void Main(string[] args)


{
int n,m;
do
{
Console.Write("Enter Value m = ");
m = int.Parse(Console.ReadLine());
Console.Write("Enter Value n = ");
n = int.Parse(Console.ReadLine());
} while (n < 0 || m < 0);

Console.WriteLine("GCD cua " + m + " and " + n + " su dung thuat toan dequi: " + GCDDeQui(m, n));
Console.WriteLine("GCD cua " + m + " and " + n + " Khong dung thuat toan dequi: " + GCD_KDeQui(m, n));
}
}

using System;

public class Program


{
public static void Main()
{
int n;
do
{
Console.Write("Nhap vao bac cua tam giac Pascal: ");
n = int.Parse(Console.ReadLine());

} while (n <= 0);


Pascal(n);
}

public static void Pascal(int n)


{
int[ , ] arr = new int[n,n];
/* n =2 . i = 0;j=0 => arr[0,0]=1
i = 1;j=0 => arr[1,0]=1
i = 1;j=1 => arr[0,0]=1 */
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == 0 || j == i)
{
arr[i, j] = 1;
}
else
{
//arr[2,2] = arr[1,1] + arr[1,2]
arr[i, j] = arr[i - 1, j - 1] + arr[i - 1, j];
}
// in ra tính toán trên 1 dòng
Console.Write(arr[i, j] + " ");
}
//xuống dòng
Console.WriteLine();
}
}
}
using System;
class Program
{
static void Main(string[] args)
{
int x, y;
Console.Write("Enter x: ");
x = int.Parse(Console.ReadLine());

Console.Write("Enter y: ");
y = int.Parse(Console.ReadLine());

Console.WriteLine(Number(x, y));

}
static string Number(int x, int y)
{
string result = "";

for (int n = x; n <= y; n++)


{
int sum = SumDivisors(n);

if (sum < n)
{
result += n + " is deficient\n";
}
else if (sum == n)
{
result += n + " is perfect\n";
}
else // sum > n
{
result += n + " is abundant\n";
}
}
return result;
}
static int SumDivisors(int n)
{
int sum = 0;

for (int i = 1; i <= n / 2; i++)


{
if (n % i == 0)
{
sum += i;
}
}

sum += n;

return sum;
}
}

Viết lại Class Carray


using System;

class CArray
{
private int[] Arr;
private int Upper;
private int NumElements;

// khởi tạo hàm CArray với kích thước là tham số size


public CArray(int size)
{
Arr = new int[size];
//upper bằng kích thước lớn nhất của mảng trừ 1
Upper = size - 1;
//Thiết lập các ptu hiện có trong mảng = 0;
NumElements = 0;
}
// chèn thêm 1 phần tử với biến là item vào cuối mảng
public void Insert(int item)
{
if (NumElements <= Upper)
{
Arr[NumElements] = item;
NumElements++;
}
else
{

Console.WriteLine("Error: Array is full.");


}
}

public void DisplayArray()


{
for (int i = 0; i < NumElements; i++)
{
Console.Write(Arr[i] + " ");
}
Console.WriteLine();
}

public void Clear()


{
for (int i = 0; i < NumElements; i++)
{
Arr[i] = 0;
}
NumElements = 0;
}
}

Tìm kiếm tuần tự

using System;

class Program
{
static void Main(string[] args)
{
Console.Write("Nhap so luong phan tu: ");
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write($"Nhap phan tu thu {i}: ");
arr[i] = int.Parse(Console.ReadLine());
}

Console.Write("Nhap so can tim: ");


int searchKey = int.Parse(Console.ReadLine());

bool found = SeqSearch(arr, searchKey);


if (found)
{
Console.WriteLine($"so {searchKey} co trong mang");
}
else
{
Console.WriteLine($"so {searchKey} khong co trong mang");
}
}

public static bool SeqSearch(int[] arr, int searchKey)


{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == searchKey)
{
return true;
}
}

return false;
}
}
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Nhap so luong phan tu: ");
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write($"Nhap phan tu thu {i}: ");
arr[i] = int.Parse(Console.ReadLine());
}

Console.Write("Nhap so can tim: ");


int searchKey = int.Parse(Console.ReadLine());
Console.Write($"vi tri {SeqSearch(arr, searchKey)}");

public static int SeqSearch(int[] arr, int searchKey)


{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == searchKey)
{
return i;
}
}

return -1;
}
}
using

System;
class Program
{
static void Main(string[] args)
{
Console.Write("Nhap so luong phan tu: ");
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write($"Nhap phan tu thu {i}: ");
arr[i] = int.Parse(Console.ReadLine());
}
int minValue = FindMin(arr);
Console.Write($"Gia tri la {minValue}");
}
static int FindMin(int[] arr)
{
int min = arr[0];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] <= min)
{
min = arr[i];
}
}
return min;
}
}
using System;

class Program
{
static void Main(string[] args)
{
Console.Write("Nhap so luong phan tu: ");
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write($"Nhap phan tu thu {i}: ");
arr[i] = int.Parse(Console.ReadLine());
}
int maxValue = FindMax(arr);
Console.Write($"Gia tri la {maxValue}");
}
static int FindMax(int[] arr)
{
int max = arr[0];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] >= max)
{
max = arr[i];

}
}
return max;

}
}

using System;
class Program
{
static void Main(string[] args)
{
// Tạo kich thước mảng
int n;
Console.Write("Size of array : ");
n = int.Parse(Console.ReadLine());
// Tạo mảng
int[] arr = new int[n];
for(int i = 0; i<arr.Length;i++ )
{
Console.Write($"Enter value at pos {i} : ");
arr[i]= int.Parse(Console.ReadLine());
}
// sắp xếp mảng
arrangearray( arr);
Console.WriteLine("array after arrangement !");
//Xuat mang da sap xep
for(int i = 0;i<arr.Length;i++ )
{
Console.WriteLine($"value at pos {i} la {arr[i]}");
}
//Tạo key muốn tìm kiếm
int keysearch;
Console.Write("Select value to search: ");
keysearch = int.Parse(Console.ReadLine());

bool valueSearch = segsearch(arr, keysearch);

if (valueSearch)
{
Console.WriteLine($"Value {keysearch} found!");
}
else
{
Console.WriteLine($"Value {keysearch} not found!");
}
}
static bool segsearch(int[] arr , int keysearch )
{
for(int i= 0; i < arr.Length;i++)
{
if (arr[i] == keysearch)
{
return true;
}
}
return false;
}
static void arrangearray(int[] arr)
{
// Sắp xếp mảng tăng dần bằng thuật toán selection sort

int swap;
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j])
{
// Hoan vi 2 so a[i] va a[j]
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
}
}
}
}

You might also like