Professional Documents
Culture Documents
EX02 NguyenAnhTu N20DCDT026
EX02 NguyenAnhTu N20DCDT026
namespace ThapHaNoi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
}
private void txtSB_TextChanged(object sender, EventArgs e)
{
// 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 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);
}
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();
}
txtN.Text = n.ToString();
}
}
}
//Tính Giai Thua
private int GiaiThua(int n)
{
if(n==0)
{
return 1;
}
else
{
return n * GiaiThua(n - 1);
}
}
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);
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;
}
}
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;
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;
Console.Write("Enter y: ");
y = int.Parse(Console.ReadLine());
Console.WriteLine(Number(x, y));
}
static string Number(int x, int y)
{
string result = "";
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;
sum += n;
return sum;
}
}
class CArray
{
private int[] Arr;
private int Upper;
private int NumElements;
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());
}
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());
}
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());
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;
}
}
}
}
}