KTLT

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

BUỔI 1

Bài 1
namespace BT1
{
class Program
{
static void NhapMang1(out int[,] a)
{
Console.Write("Nhap so dong : ");
int row = int.Parse(Console.ReadLine());
Console.Write("Nhap so cot : ");
int col = int.Parse(Console.ReadLine());
a = new int[row, col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write("a[{0},{1}]: ", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static int[,] NhapMang2()
{
int[,] b;
Console.Write("Nhap so dong : ");
int row = int.Parse(Console.ReadLine());
Console.Write("Nhap so cot : ");
int col = int.Parse(Console.ReadLine());
b = new int[row, col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write("b[{0},{1}]: ", i, j);
b[i, j] = int.Parse(Console.ReadLine());
}
}
return b;
}

static void NhapMang3(int[,]c)


{
for (int i = 0; i<c.GetLength(0); i++)
{
for (int j = 0; j<c.GetLength(1); j++)
{
Console.Write("c[{0},{1}]:", i, j);
c[i, j] = int.Parse(Console.ReadLine());
}
}

}
static void XuatMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static void Main ()

1
{
Console.WriteLine("Nhap mang a ");
int[,] a;
NhapMang1 (out a);
Console.WriteLine("Mang a vua nhap : ");
XuatMang(a);
Console.WriteLine("Nhap mang b ");
int[,] b;
b = NhapMang2();
Console.WriteLine("Mang b vua nhap : ");
XuatMang(b);
Console.WriteLine("Nhap mang c : ");
Console.Write("Nhap so dong : ");
int row = int.Parse(Console.ReadLine());
Console.Write("Nhap so cot : ");
int col =int.Parse(Console.ReadLine());
int[,]c=new int[row,col];
NhapMang3(c);
Console.WriteLine("Mang c vua nhap ");
XuatMang(c);
}
}
}

Bài 2
namespace BT2
{
class Program
{
static void NhapMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("a[{0},{1}] = ", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,] a)
{
Console.WriteLine("Mang vua nhap la");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static void InDongk(int[,] a,int k)
{
Console.Write("Dong thu {0} la : ", k);
if(k<0||k>=a.GetLength(0))
{
Console.WriteLine("Invalid k");
}
else
{
for (int i = 0; i < a.GetLength(1); i++)
{
Console.Write(a[k,i] + " " );
}
}
}
static void Main(string[]args)

2
{
int[,] a;
Console.Write("Nhap so dong, so cot va k: ");
string line = Console.ReadLine();
string[] nums = line.Split(' ');
int row = int.Parse(nums[0]);
int col = int.Parse(nums[1]);
int k = int.Parse(nums[2]);
a=new int[row,col];
NhapMang(a);
XuatMang(a);
InDongk(a,k);
}
}
}

Bài 3
namespace BT3
{
class Program
{
static void NhapMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("a[{0},{1}] =", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,] a)
{
Console.WriteLine("Mang vua nhap la");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static void InCotk(int[,] a, int k)
{
Console.WriteLine("Cot thu {0} la : ", k);
if (k < 0 || k >= a.GetLength(1))
{
Console.WriteLine("Invalid k");
}
else
{
for (int i = 0; i < a.GetLength(0); i++)
{
Console.WriteLine(a[i, k]);
}
}
}
static void Main(string[] args)
{
int[,] a;
Console.Write("Nhap so dong, so cot va k: ");
string line = Console.ReadLine();
string[] nums = line.Split(' ');
int row = int.Parse(nums[0]);
int col = int.Parse(nums[1]);
int k = int.Parse(nums[2]);

3
a = new int[row, col];
NhapMang(a);
XuatMang(a);
InCotk(a, k);
}
}
}

Bài 4
namespace BT4
{
class Program
{
static void NhapMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("a[{0},{1}] =", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,] a)
{
Console.WriteLine("Mang vua nhap la");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static void TongTrenDongk(int[,] a, int k)
{
int sumd = 0;
Console.Write("Tong tren dong thu {0} la : ", k);
if (k < 0 || k >= a.GetLength(0))
{
Console.WriteLine("Invalid k");
}
else
{
for (int i = 0; i < a.GetLength(1); i++)
{
sumd += a[k,i];
}
Console.WriteLine(sumd);
}
}
static void TongTrenCotk(int[,] a, int k)
{
int sumc = 0;
Console.Write("Tong tren cot thu {0} la : ", k);
if (k < 0 || k >= a.GetLength(1))
{
Console.WriteLine("Invalid k");
}
else
{
for (int i = 0; i < a.GetLength(0); i++)
{
sumc += a[i,k];
}
Console.WriteLine(sumc);
}

4
}
static void Tong(int[,] a, int k)
{
int sum = 0;
Console.Write("Tong cac phan tu la : ");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
sum += a[i,j];
}
}
Console.WriteLine(sum);
}
static void TongChan(int[,] a, int k)
{
int sumchan = 0;
Console.Write("Tong cac phan tu chan la : ");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
if (a[i,j]%2 == 0)
sumchan += a[i, j];
}
}
Console.WriteLine(sumchan);
}
static void TongLe(int[,] a, int k)
{
int sumle = 0;
Console.Write("Tong cac phan tu chan la : ");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
if (a[i, j] % 2 != 0)
sumle += a[i, j];
}
}
Console.WriteLine(sumle);
}
static void TB(int[,] a, int k)
{
int sum = 0;
Console.Write("Trung binh cac phan tu la : ");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
sum += a[i, j];
}
}
double tb = (double)sum/a.Length;
Console.WriteLine(tb);
}

static void Main(string[] args)


{
int[,] a;
Console.Write("Nhap so dong, so cot va k: ");
string line = Console.ReadLine();
string[] nums = line.Split(' ');
int row = int.Parse(nums[0]);
int col = int.Parse(nums[1]);
int k = int.Parse(nums[2]);
a = new int[row, col];
NhapMang(a);
XuatMang(a);
TongTrenDongk(a, k);

5
TongTrenCotk(a, k);
Tong(a, k);
TongChan(a, k);
TongLe(a, k);
TB(a,k);

}
}
}

Bài 5
using System;
namespace BT5
{
class Program
{
static void NhapMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("a[{0},{1}] =", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,] a)
{
Console.WriteLine("Mang vua nhap la");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static void MaxD(int[,] a, int k)
{
Console.Write("Phan tu lon nhat dong thu {0} la : ", k);
if (k < 0 || k >= a.GetLength(0))
{
Console.WriteLine("Invalid k");
}
else
{
int max = a[k, 0];
for (int i = 1; i < a.GetLength(1); i++)
{
if(max< a[k, i])
{
max= a[k, i];
}
}
Console.Write(max);
}
}
static void MinC(int[,] a, int k)
{
Console.Write("Phan tu nho nhat cot thu {0} la : ", k);
if (k < 0 || k >= a.GetLength(1))
{
Console.WriteLine("Invalid k");
}
else
{
int min = a[0, k];

6
for (int i = 1; i < a.GetLength(0); i++)
{
if (min > a[i, k])
{
min = a[i,k];
}
}
Console.Write(min);
}
}
static bool KtSoNt(int a)
{
if(a<2)
{
return false;
}
for ( int i = 2; i <a;i++)
{
if (a%i==0)
{
return false;
}
}
return true;
}
static void SoNt(int[,] a)
{
Console.Write("Cac so nguyen to trong mang la : ");
for(int i = 0;i<a.GetLength(0);i++)
{
for(int j = 0;j<a.GetLength(1);j++)
{
if (KtSoNt(a[i,j]))
{
Console.Write(a[i, j] + " ");
}
}
}
}
static void Main(string[] args)
{
int[,] a;
Console.Write("Nhap so dong, so cot va k: ");
string line = Console.ReadLine();
string[] nums = line.Split(' ');
int row = int.Parse(nums[0]);
int col = int.Parse(nums[1]);
int k = int.Parse(nums[2]);
a = new int[row, col];
NhapMang(a);
XuatMang(a);
MaxD(a,k);
Console.WriteLine();
MinC(a,k);
Console.WriteLine();
SoNt(a);
}
}
}

Bài 6
namespace BT6
{
internal class Program
{
static void NhapMang(int[,]a)
{

7
for(int i = 0; i <a.GetLength(0); i++)
{
for (int j = 0; j<a.GetLength(1);j++)
{
Console.Write($"a[{i},{j}] = ");
a[i,j]=int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,]a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static void DongTang( int[,]a)
{
Console.Write("Nhap k : ");
int k = int.Parse(Console.ReadLine());
for(int i = 0; i< a.GetLength(1);i++)
{
for (int j = i+1;j<a.GetLength(1);j++ )
{
if (a[k, i] > a[k,j])
{
int temp = a[k,i];
a[k,i]= a[k,j];
a[k,j]= temp;
}

}
}
static void CotGiam(int[,] a)
{
Console.Write("Nhap k : ");
int k = int.Parse(Console.ReadLine());
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = i + 1; j < a.GetLength(0); j++)
{
if (a[i, k] < a[j, k])
{
int temp = a[i, k];
a[i, k] = a[j, k];
a[j, k] = temp;
}

}
}
static void Main(string[] args)
{
Console.Write("Nhap so dong : ");
int m = int.Parse(Console.ReadLine());
Console.Write("Nhap so cot : ");
int n = int.Parse(Console.ReadLine());
int [,]a = new int[m, n];
NhapMang(a);
XuatMang(a);
DongTang(a);
XuatMang(a);
CotGiam(a);

8
XuatMang(a);
Console.ReadKey();
}
}
}

Bài 7
using System;
namespace BT7
{
class Program
{
static void NhapMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("a[{0},{1}] =", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,] a)
{
Console.WriteLine("Mang vua nhap la");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static void CheoChinh(int[,] a)
{
Console.Write("Cac phan tu tren duong cheo chinh : ");
for (int i = 0; i < a.GetLength(0); i++)
{
for(int j = 0;j< a.GetLength(1); j++)
{
if(i==j)
{
Console.Write(a[i,j]+" ") ;
}
}
}
Console.WriteLine();
}
static void CheoPhu(int[,] a)
{
Console.Write("Cac phan tu tren duong cheo phu : ");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
if (i + j == a.GetLength(0)-1)
{
Console.Write(a[i,j] + " ");
}
}
}
Console.WriteLine();
}
static void TamGiacTren(int[,] a)
{
Console.Write("Cac phan tu cua tam giac tren : ");

9
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
if (i <=j)
{
Console.Write(a[i, j]+ " ");
}
}
Console.WriteLine();
}
static void Main(string[] args)
{
int[,] a;
Console.Write("Nhap so dong, so cot : ");
string line = Console.ReadLine();
string[] nums = line.Split(' ');
int row = int.Parse(nums[0]);
int col = int.Parse(nums[1]);
a = new int[row, col];
NhapMang(a);
XuatMang(a);
if(row==col)
{
CheoChinh(a);
CheoPhu(a);
TamGiacTren(a);
}
else
{
Console.Write("Khong thoa la ma tran vuong");
}

}
}
}

BUỔI 2
Bài 1
using System;
namespace BT1
{
class Program
{
static void NhapMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("[{0},{1}] =", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}

10
}
static void TinhTong(int[,] a, int[,] b, int[,]c)
{
Console.WriteLine("Tong hai ma tran ");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
c[i, j] = a[i,j]+ b[i, j];
}
}
}
static void Main(string[] args)
{
Console.Write("Nhap m va n : ");
string input = Console.ReadLine();
string[] ds = input.Split(' ');
int m = int.Parse(ds[0]);
int n = int.Parse(ds[1]);
Console.WriteLine(" m = " + m);
Console.WriteLine(" n = " + n);
int[,]a= new int[m,n];
int[,]b= new int[m,n];
int[,]c = new int[m, n];
NhapMang(a);
XuatMang(a);
NhapMang(b);
XuatMang(b);
TinhTong(a,b,c);
XuatMang(c);

}
}
}

Bài 2
using System;
namespace BT2
{
class Program
{
static void NhapMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("[{0},{1}] =", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static int DotProduct(int[,] a, int[,]b,int i,int j)
{
int sum = 0;

11
for(int k = 0; k < a.GetLength(1);k++)
{
sum = sum + a[i, k] * b[k, j];
}
return sum;
}
static void TinhTich(int[,] a, int[,] b, int[,]c)
{
Console.WriteLine("Tich hai ma tran ");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
c[i, j] = DotProduct(a,b,i,j);
}
}
}
static void Main(string[] args)
{
Console.Write("Nhap m, n va p : ");
string input = Console.ReadLine();
string[] ds = input.Split(' ');
int m = int.Parse(ds[0]);
int n = int.Parse(ds[1]);
int p = int.Parse(ds[2]);
Console.WriteLine(" m = " + m);
Console.WriteLine(" n = " + n);
Console.WriteLine(" p = " + p);
int[,]a= new int[m,n];
int[,]b= new int[n,p];
int[,]c = new int[m,p];
NhapMang(a);
XuatMang(a);
NhapMang(b);
XuatMang(b);
TinhTich(a,b,c);
XuatMang(c);

}
}
}

Bài 3
using System;

namespace BT3
{
class Program
{
static void NhapGiaTienSP(int[]a)
{
for( int i = 0;i<a.Length;i++ )
{
Console.Write($"Gia tien cua san pham thu {i} : ");
a[i] = int.Parse(Console.ReadLine());
}
}
static void NhapSoLuongSPTheoNgay(int[,]b)
{
for(int i = 0; i< b.GetLength(0); i++)
{
for( int j = 0; j< b.GetLength(1); j++)
{
Console.Write($"San pham thu {i} ngay thu {j} da ban duoc so luong la : ");
b[i,j]=int.Parse(Console.ReadLine());
}
}
}

12
static int DotProduct(int[] a, int[,]b,int j )
{
int sum = 0;
for(int k = 0; k<a.Length; k++ )
{
sum = sum + a[k] * b[k,j];
}
return sum;
}
static void NhanHaiMaTran(int[] a, int[,]b,int[]c)
{
for (int j = 0; j< c.Length;j++)
{
c[j] = DotProduct(a,b,j);
}
}
static void XuatMang(int[] c)
{
Console.WriteLine("Tong so tien ban duoc moi ngay theo thu tu : ");
for (int j = 0; j < c.Length; j++)
{
Console.Write("{0,5}", c[j]);
}
}
static int Tong(int[] c)
{
int sum = 0;
for( int j = 0; j< c.Length;j++)
{
sum =sum + c[j];
}
return sum;
}
static void Main(string[] args)
{
int m, n;
Console.Write("So san pham cong ty ban : ");
m = int.Parse(Console.ReadLine());
int[]a= new int[m];
NhapGiaTienSP(a);
Console.Write("Cong ty da ban bao nhieu ngay : ");
n = int.Parse(Console.ReadLine());
int[,]b = new int[m,n];
int []c = new int[n];
Console.WriteLine();
NhapSoLuongSPTheoNgay(b);
Console.WriteLine();
NhanHaiMaTran(a, b, c);
XuatMang(c);
Console.WriteLine();
Console.Write($"Tong so tien thu duoc trong {n} ngay : {Tong(c)}");
Console.WriteLine();
}
}
}

Bài 4
using System;
namespace BT4
{
class Program
{
static void NhapMang(int[,] a)
{

13
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("[{0},{1}] =", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static void ChuyenVi(int[,] a, int[,] b)
{
Console.WriteLine("Chuyen vi ");
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
b[i, j] = a[j,i];
}
}
}
static void Main(string[] args)
{
Console.Write("Nhap m va n: ");
string input = Console.ReadLine();
string[] ds = input.Split(' ');
int m = int.Parse(ds[0]);
int n = int.Parse(ds[1]);
Console.WriteLine(" m = " + m);
Console.WriteLine(" n = " + n);
int[,]a= new int[m,n];
int[,]b= new int[n,m];
NhapMang(a);
XuatMang(a);
ChuyenVi(a,b);
XuatMang(b);

}
}
}

Bài 5
using System;
namespace BT5
{
class Program
{
static void NhapMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("[{0},{1}] =", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}

14
}
}
static void XuatMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static int Tinh(int[,] a, int[,] b)
{
int sum = 0;
for ( int i = 0; i < a.GetLength(0); i++)
{
for ( int j = 0; j < a.GetLength(1); j++ )
{
sum =sum + ( a[i,j] - b[i, j]) * (a[i, j] - b[i, j]);
}
}
return sum ;
}
static void Main(string[] args)
{
Console.Write("Nhap m va n: ");
string input = Console.ReadLine();
string[] ds = input.Split(' ');
int m = int.Parse(ds[0]);
int n = int.Parse(ds[1]);
Console.WriteLine(" m = " + m);
Console.WriteLine(" n = " + n);
int[,] a = new int[m, n];
int[,] b = new int[n, m];
NhapMang(a);
XuatMang(a);
NhapMang(b);
XuatMang(b);
double dist = Math.Sqrt(Tinh(a, b));
Console.Write($"Khoang cach 2 ma tran la :{dist:F2} ");
}
}
}

Bài 6
using System;
namespace BT6
{
class Program
{
static void NhapMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("[{0},{1}] =", i, j);
a[i, j] = int.Parse(Console.ReadLine());
}
}
}
static void XuatMang(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)

15
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,3}", a[i, j]);
}
Console.WriteLine();
}
}
static int DotProduct(int[,] a, int[,] b)
{
int value = 0;
for ( int i = 0; i < a.GetLength(0); i++)
{
for ( int j = 0; j < a.GetLength(1); j++ )
{
value = value + (a[i, j] * b[i, j]);
}
}
return value;
}
static void Main(string[] args)
{
Console.Write("Nhap n: ");
int n = int.Parse(Console.ReadLine());
Console.WriteLine(" n = " + n);
int[,] a = new int[n, n];
int[,] b = new int[n, n];
NhapMang(a);
XuatMang(a);
NhapMang(b);
XuatMang(b);
Console.Write($"Value :{DotProduct(a,b)} ");
}
}
}

Bài 7
using System;

namespace Buoi6
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
int m, n, k;
Console.Write("Bạn hãy nhập số dòng và cột của ma trận a: ");
string input = Console.ReadLine();
string[] ds = input.Split(' ');
m = int.Parse(ds[0]);
n = int.Parse(ds[1]);
k = int.Parse(ds[2]);

int[,] a = new int[m, n];

Console.WriteLine("Nhập mảng a");


NhapMang(a);
Console.WriteLine("Mảng a vừa nhập");
XuatMang(a);

int[,] b = new int[k, k];


Console.WriteLine("Nhập mảng b");
NhapMang(b);
Console.WriteLine("Mảng b vừa nhập");
XuatMang(b);

16
int[,] kq = new int[m-k+1, n-k+1];
//int[,] kq = new int[m, n];
//Console.WriteLine("Dot Product của 2 ma trận là: " + DotProduct(a, b).ToString());
//TinhMangTichChap(b, a, kq);
Convolute(kq, a, b);
Console.WriteLine("Mảng kết quả tích chập");
XuatMang(kq);
Console.ReadKey();
}
static void Convolute(int[,] output, int[,] input, int[,] kernel)
{
int convolute = 0; //tổng Dot product (Tích vô hướng) của ma trận mỗi lần chạy
int x, y;//sử dụng để làm index cho ma trận kết quả
//Điền vào mà trận
int row = input.GetLength(0);
int col = input.GetLength(1);
int ke = kernel.GetLength(0); //dòng hay cột kernel
for (int i=0;i<row;i++)
{
for (int j = 0; j < col; j++)
{
if (j < output.GetLength(1)&&i< output.GetLength(0))
{
x = i;
y = j;
for (int k = 0; k < ke; k++)
{
for (int l = 0; l < ke; l++)
{
//tích chập
convolute += kernel[k, l] * input[x, y];
y++;

}
x++;//di chuyển xuống
y = j; //restart lại cột
}
//}
output[i, j] = convolute;
convolute = 0; //lấy lại index từ đầu
}
}
}
}
public static void MangTichChap(int[,] a, int[,] b)
{
int ca = a.GetLength(0);
int cb = b.GetLength(0);
for(int i=0;i<i+cb;i++)
{
if (i + cb > ca)
break;
else
{
for (int j = 0; j < j + cb; j++)
{

}
}
}

}
public static int DotProduct(int[,] a, int[,] b)
{
int sum = 0;
for(int i=0;i<a.GetLength(0);i++)
{
for (int j = 0; j < a.GetLength(0); j++)
{
sum += a[i, j] * b[i, j];

17
}
}
return sum;
}
public static void NhapMang(int[,] c)
{
for (int i = 0; i < c.GetLength(0); i++)
{
for (int j = 0; j < c.GetLength(1); j++)
{
Console.Write("Nhập giá trị phần tử thứ [{0},{1}]: ", i, j);
c[i, j] = int.Parse(Console.ReadLine());
}
}
}
public static void XuatMang(int[,] a)
{
Console.WriteLine();

for (int i = 0; i < a.GetLength(0); i++)


{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0,4}", a[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();

}
}
}

BUỔI 3
Bài 1
using System;
namespace BT1
{
class Program
{
static int GiaiThua(int n)
{
if (n <= 1)
{
return 1;
}
else
{
return n * GiaiThua(n - 1);
}
}
static void Main(string[] args)
{
int n;
do
{
Console.Write("Nhap n : ");
n = int.Parse(Console.ReadLine());
}
while (n < 0 || n > 12);
Console.Write($"{n}! = {GiaiThua(n)}");
}
}
}

18
Bài 2
using System;
namespace BT2
{
class Program
{
static int LuyThua(int a,int n)
{
if (n == 0)
{
return 1;
}
else if(n%2 == 0)
{
return LuyThua(a, n / 2) * LuyThua(a,n/2);
}
else
{
return a * LuyThua(a, (n - 1) / 2) * LuyThua(a, (n - 1) / 2);
}
}
static void Main(string[] args)
{
int a,n;
do
{
Console.Write("Nhap a : ");
a = int.Parse(Console.ReadLine());
Console.Write("Nhap n : ");
n = int.Parse(Console.ReadLine());
}
while (a <= 0 || n < 0);
Console.Write($"{a}^{n} = {LuyThua(a,n)}");
}
}
}

Bài 3
using System;
namespace BT3
{
class Program
{
static int UCLN(int m,int n)
{
if (n == 0)
{
return m;
}
else
{
return UCLN(n,m%n);
}
}
static void Main(string[] args)
{
int m,n;
do
{
Console.Write("Nhap m : ");
m = int.Parse(Console.ReadLine());
Console.Write("Nhap n : ");

19
n = int.Parse(Console.ReadLine());
}
while (m <= 0 || n <= 0);
Console.Write($"UCLN cua {m} va {n} la {UCLN(m,n)}");
}
}
}

Bài 4
using System;
namespace BT4
{
class Program
{
static void NhiPhan(int n)
{
if (n != 0)
{
NhiPhan(n/2);
Console.Write(n % 2);
}
}
static void Main(string[] args)
{
int n;
do
{
Console.Write("Nhap n : ");
n = int.Parse(Console.ReadLine());
}
while (n <= 0);
Console.Write($"Ket qua chuyen doi cua {n} la ");
NhiPhan(n);

}
}
}

Bài 5
using System;
namespace BT5
{
class Program
{
static int DoiTho(int n)
{
if (n == 1 || n == 2)
return 1;
else
return DoiTho(n-1)+DoiTho(n-2);

}
static void Main(string[] args)
{
int n;
do
{
Console.Write("Nhap n : ");
n = int.Parse(Console.ReadLine());
}
while (n <= 0);
Console.Write($"So doi tho o thang thu {n} la : {DoiTho(n)}");
}

20
}
}

BUỔI 4
Bài 1
using System;

namespace BT1
{
class Program
{
static void NhapMang(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.Write($"a[{i}] = ");
a[i] = int.Parse(Console.ReadLine());
}
}
static void XuatMang(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
}
static void TimMaxMin(int[] a, int left, int right, ref int min, ref int max)
{
int mind = a[left];
int mins = a[right];
int maxd = a[left];
int maxs = a[right];
int k = (left + right) / 2;
if (left == right)
{
min = a[left];
max = a[left];
}
else
{

TimMaxMin(a, left, k, ref mind, ref maxd);


TimMaxMin(a, k + 1, right, ref mins, ref maxs);
}
min = mind > mins ? mins : mind;
max = maxd > maxs ? maxd : maxs;

}
static void Main(string[] args)
{
Console.Write("Nhap so phan tu : ");
int n = int.Parse(Console.ReadLine());
int[] a = new int[n];
NhapMang(a);
XuatMang(a);
int min = a[0];
int max = a[0];
Console.WriteLine();
TimMaxMin(a, 0, n - 1, ref min, ref max);
Console.Write("Max = " + max);
Console.WriteLine();
Console.Write("Min = " + min);
}
}
}

21
Bài 2
using System;

namespace BT2
{
class Program
{
static void NhapMang(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.Write($"a[{i}] = ");
a[i] = int.Parse(Console.ReadLine());
}
}
static void XuatMang(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
}
static void Tim(int[] a, int left, int right, int x)
{
int mid = (left + right) / 2;
if (left > right)
{
Console.Write($"Khong tim thay so {x} ");
}
else
{

if (a[mid] == x)
{
Console.Write($"So {x} o vi tri : {mid}");
}
else if (x < a[mid])
{
Tim(a, left, mid - 1, x);
}
else
{
Tim(a, mid + 1, right, x);
}

}
static void Main(string[] args)
{
Console.Write("Nhap so phan tu : ");
int n = int.Parse(Console.ReadLine());
int[] a = new int[n];
NhapMang(a);
XuatMang(a);
Console.WriteLine();
Console.Write("Nhap x : ");
int x = int.Parse(Console.ReadLine());
Tim(a, 0, n - 1, x);
Console.WriteLine();
}
}
}

22
Bài 3
using System;

namespace BT3
{
class Program
{
static void ThapHN(int n, char nguon, char dich, char tg)
{
if (n == 1)
{
Console.WriteLine();
Console.Write($"Chuyen 1 dia tu cot {nguon} --> {dich}");
}
else
{
ThapHN(n - 1, nguon, tg, dich);
Console.WriteLine();
Console.Write($"Chuyen 1 dia tu cot {nguon} --> {dich}");
ThapHN(n - 1, tg, dich, nguon);
}
}
static void Main(string[] args)
{
Console.Write("Nhap so dia : ");
int n = int.Parse(Console.ReadLine());
char a = 'A', b = 'B', c = 'C';
ThapHN(n, a, c, b);
Console.WriteLine();
}
}
}

BUỔI 5
Bài 1( lặp)
using System;

namespace BT1
{
class Program
{
static int n, k;
static int[] A = new int[100];
static int dem;
static void XuatMang()
{
for (int i = 1; i <= k; i++)
{
Console.Write(A[i] + " ");
}
Console.WriteLine();
}
static void ChinhHop(int i)
{
for (int j = 1; j <= n; j++)
{
A[i] = j;
if (i == k)
{
dem++;
XuatMang();

23
}
else
{
ChinhHop(i + 1);
}
}
}
static void Main(string[] args)
{
do
{
Console.Write("Nhap n : ");
n = int.Parse(Console.ReadLine());
Console.Write("Nhap k : ");
k = int.Parse(Console.ReadLine());
}
while (n < k);
dem = 0;
ChinhHop(1);
Console.Write("So luong chinh hop : " + dem);

}
}
}

Bài 2( không lặp)


using System;

namespace BT2
{
class Program
{
static int n, k;
static int[] A = new int[100];
static int[] C = new int[100];
static int dem;
static void XuatMang()
{
for (int i = 1; i <= k; i++)
{
Console.Write(A[i] + " ");
}
Console.WriteLine();
}
static void ChinhHop(int i)
{
for (int j = 1; j <= n; j++)
{
if (C[j] == 0)
{
A[i] = j;
if (i == k)
{
dem++;
XuatMang();
}
else
{
C[j] = 1;
ChinhHop(i + 1);
C[j] = 0;
}
}
}
}
static void Main(string[] args)
{

24
do
{
Console.Write("Nhap n : ");
n = int.Parse(Console.ReadLine());
Console.Write("Nhap k : ");
k = int.Parse(Console.ReadLine());
}
while (n < k);
dem = 0;
ChinhHop(1);
Console.Write("So luong chinh hop : " + dem);

}
}
}

Bài 3
using System;

namespace BT3
{
class Program
{
static int n, k;
static int[] A = new int[100];
static int[] C = new int[100];
static int dem;
static void XuatMang()
{
for (int i = 1; i <= k; i++)
{
Console.Write(A[i] + " ");
}
Console.WriteLine();
}
static void ToHop(int i)
{
for (int j = A[i - 1] + 1; j <= n; j++)
{

A[i] = j;
if (i == k)
{
dem++;
XuatMang();
}
else
{
C[j] = 1;
ToHop(i + 1);
C[j] = 0;
}

}
}
static void Main(string[] args)
{
do
{
Console.Write("Nhap n : ");
n = int.Parse(Console.ReadLine());
Console.Write("Nhap k : ");
k = int.Parse(Console.ReadLine());
}
while (n < k);
dem = 0;

25
ToHop(1);
Console.Write("So luong to hop : " + dem);
Console.ReadKey();
}
}
}

Bài 4
using System;

namespace BT4
{
class Program
{
static int N = 8;
static int[] X = new int[100];
static int[] cot = new int[100];
static int[] d1 = new int[100];
static int[] d2 = new int[100];
static int dem = 0;
static void inkq()
{
for (int i = 1; i <= N; i++)
{
Console.Write(X[i] + " ");
}
Console.WriteLine();
}
static void Try(int i)
{
for (int j = 1; j <= N; j++)
{
if (cot[j] == 0 && d1[i - j + N] == 0 && d2[i + j - 1] == 0)
{
X[i] = j;
cot[j] = d1[i - j + N] = d2[i + j - 1] = 1;
if (i == N)
{
dem++;
Console.Write("Solution " + dem + " :");
inkq();
}
else
{
Try(i + 1);
}
cot[j] = d1[i - j + N] = d2[i + j - 1] = 0;
}
}

}
static void Main(string[] args)
{
Try(1);

Console.ReadKey();
}
}
}

Bài 5

26
BUỔI 6
Bài 1
internal class Student
{
private string studentID;
private string name;
private int birthYear;
private bool gender;
private string stdClass;
public Student() { }
public Student(string studentID, string name, int birthYear, bool gender, string stdClass)
{
this.studentID = studentID;
this.name = name;
this.birthYear = birthYear;
this.gender = gender;
this. stdClass = stdClass;
}
public string GetStudentID()
{
return studentID;
}
public void SetStudentID(string newID)
{
studentID = newID;
}
public string GetName()
{
return name;
}
public void SetName(string newName)
{
name = newName;
}
public int GetBirthYear()
{
return birthYear;
}
public void SetBirthYear(int newBirthYear)
{
if (newBirthYear < 1900 || newBirthYear > DateTime.Now.Year)
{
Console.WriteLine("Năm không hợp lệ!!!!");
birthYear = 1900;
}
else
{
birthYear = newBirthYear;
}
}
public bool GetGender()
{
return gender;
}
public void SetGender(bool newGender)
{
gender = newGender;
}
public string GetStdClass()
{
return stdClass;
}
public void SetStdClass( string newStdClass )
{
stdClass = newStdClass;

27
}
public void Input()
{
Console.Write("Nhap MSSV : ");
SetStudentID(Console.ReadLine());
Console.Write("Nhap ten : ");
SetName(Console.ReadLine());
Console.Write("Nhap nam sinh : ");
SetBirthYear(int.Parse(Console.ReadLine()));
Console.Write("Nhap gioi tinh : ");
SetGender(bool.Parse(Console.ReadLine()));
Console.Write("Nhap lop : ");
SetStdClass(Console.ReadLine());
}
public int GetAge ()
{
return DateTime.Now.Year - birthYear;
}
public void PrintInfo()
{
Console.WriteLine("Student ID : " + studentID);
Console.WriteLine("Name : " + name);
Console.WriteLine("BirthYear : " + birthYear);
if (gender == true)
Console.WriteLine("Giới tính: Nam");
else
Console.WriteLine("Giới tính: Nữ");
Console.WriteLine("Student Class : " + stdClass);
}
}

internal class Program


{
static void Main(string[] args)
{
Console.Write("Nhap ngay : ");
int day =int.Parse(Console.ReadLine());
Console.Write("Nhap thang : ");
int month = int.Parse(Console.ReadLine());
Console.Write("Nhap nam : ");
int year = int.Parse(Console.ReadLine());
Date date= new Date(day,month,year);
Console.Write(date.ToString());

}
}

Bài 2
internal class Fraction
{
private int numerator;
private int denominator;
public int Numerator
{
get { return numerator; }
set
{
numerator = value;
}
}
public int Denominator
{
get { return denominator; }
set
{
if (value != 0)
{
denominator = value;

28
}
else
{
Console.WriteLine("Mau so phai khac 0. " +
"Mau so duoc thiet lap mac dinh la 1");
denominator = 1;
}
}
}
public Fraction()
{
this.numerator = 0;
this.denominator = 1;
}
public Fraction(int numerator, int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
this.Simplify();
}
public Fraction(int numerator)
{
this.numerator = numerator;
this.denominator = 1;
}
private int UCLN(int a , int b)
{
if (b == 0)
return a;
return UCLN(b, a % b);
}
private void Simplify()
{
int uc = UCLN(this.numerator,this.denominator);
numerator = numerator/uc;
denominator = denominator/uc;
}
public void Input()
{
Console.Write("Nhap tu so : ");
Numerator=int.Parse(Console.ReadLine());
Console.Write("Nhap mau so : ");
Denominator=int.Parse(Console.ReadLine());
this.Simplify();
}
public double Decimal ()
{
return (double) numerator / denominator;
}
public Fraction Add(Fraction p)
{
Fraction sum = new Fraction();
sum.Denominator = denominator * p.denominator;
sum.Numerator = numerator * p.denominator + denominator * p.numerator;
sum.Simplify();
return sum;
}
public Fraction Subtract(Fraction p)
{
Fraction tru = new Fraction();
tru.Denominator = denominator * p.denominator;
tru.Numerator = numerator * p.denominator - denominator * p.numerator;
tru.Simplify();
return tru;
}
public Fraction Multiply(Fraction p)
{
Fraction nhan = new Fraction();
nhan.Denominator = denominator * p.denominator;
nhan.Numerator = numerator * p.numerator;

29
nhan.Simplify();
return nhan;
}
public Fraction Divide(Fraction p)
{
Fraction chia = new Fraction();
chia.Denominator = denominator * p.numerator;
chia.Numerator= numerator *p.denominator;
chia.Simplify();
return chia;
}
public string ToString()
{
return $"{numerator} / {denominator}";
}
}

internal class Program


{
static void Main(string[] args)
{
Fraction ps1= new Fraction();
Console.WriteLine("====Phan so thu 1====");
ps1.Input();
Console.WriteLine($"Phan so thu 1 la { ps1.ToString()}");
Console.WriteLine($"Dang thap phan la {ps1.Decimal():F02}");
Fraction ps2 = new Fraction();
Console.WriteLine("====Phan so thu 2====");
ps2.Input();
Console.WriteLine($"Phan so thu 2 la {ps2.ToString()}");
Console.WriteLine($"Dang thap phan la {ps2.Decimal():F02}");
Console.WriteLine();
Console.WriteLine($"Tong 2 pham so la {ps1.Add(ps2).ToString()} ");
Console.WriteLine($"Hieu 2 pham so la {ps1.Subtract(ps2).ToString()}");
Console.WriteLine($"Tich 2 pham so la {ps1.Multiply(ps2).ToString()}");
Console.WriteLine($"Thuong 2 pham so la {ps1.Divide(ps2).ToString()}");
Console.ReadKey();
}
}

Bài 3
internal class Rectangle
{
private double width;
private double height;

public Rectangle() { }
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double Width
{
get { return width; }
set
{
width = value;
while (width < 0)
{
Console.Write("Nhap chieu rong : ");
width = double.Parse(Console.ReadLine());
}
}
}
public double Height

30
{
get { return height; }
set
{
height = value;
while (height < 0)
{
Console.Write("Nhap chieu dai : ");
height = double.Parse(Console.ReadLine());
}
}
}
public void Input()
{
Console.Write("Nhap chieu rong : ");
Width=double.Parse(Console.ReadLine());
Console.Write("Nhap chieu dai : ");
Height=double.Parse(Console.ReadLine());
}
public string ToString()
{
return $"===Hinh chu nhat [Chieu dai = {Height}, Chieu rong = {Width}]====" ;
}
public double GetPerimeter()
{
return (width+height)*2;
}
public double GetArea()
{
return width * height;
}
public bool IsSameArea(Rectangle rect)
{
if (width ==rect.Width && height==rect.height)
{
return true;
}
else
{
return false;
}
}

internal class Program


{
static void Main(string[] args)
{
Rectangle rect = new Rectangle();
rect.Input();
Console.WriteLine(rect.ToString());
Console.WriteLine($"Chu vi = {rect.GetPerimeter()}");
Console.WriteLine($"Dien tich = {rect.GetArea()}");
}
}

Bài 4
internal class Date
{
private int day;
private int month;
private int year;
public Date(int day, int month, int year)
{
this.day = day;

31
this.month = month;
this.year = year;
}
public int getDay()
{
return day;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
public void setDay(int newDay)
{
day = newDay;
}
public void setMonth( int newMonth)
{

month = newMonth;
}
public void setYear( int newYear)
{
year = newYear;
}
public void SetDate(int day, int month, int year)
{

while (day < 0 || day > 31)


{
Console.Write("Nhap lai nam : ");
day = int.Parse(Console.ReadLine());
}
this.day = day;
while (month < 0 || month > 12)
{
Console.Write("Nhap lai thang : ");
month = int.Parse(Console.ReadLine());
}
this.month = month;
while (year < 1990 || year > 9999)
{
Console.Write("Nhap lai nam : ");
year = int.Parse(Console.ReadLine());
}
this.year = year;
}
public string ToString()
{
SetDate(day, month, year);
return $"{day:D2}/{month:D2}/{year}";
}

internal class Program


{
static void Main(string[] args)
{
Console.Write("Nhap ngay : ");
int day =int.Parse(Console.ReadLine());
Console.Write("Nhap thang : ");
int month = int.Parse(Console.ReadLine());
Console.Write("Nhap nam : ");
int year = int.Parse(Console.ReadLine());
Date date= new Date(day,month,year);
Console.Write(date.ToString());

32
}
}

Bài 5
internal class Author
{
private string name;
private string email;
private char gender;
public Author(string name, string email, char gender)
{
this.name = name;
this.email = email;
this.gender = gender;
}
public string getName()
{
return name;
}
public string getEmail()
{
return email;
}
public char getGender()
{
return gender;
}
public string toString()
{
return $"Author[name = {name},email = {email}, gender = {gender}]";
}
}

internal class Book


{
private string name;
private Author author;
private double price;
private int qty = 0;
public Book(string name, Author author, double price)
{
this.name = name;
this.author = author;
this.price = price;
}
public Book(string name, Author author, double price, int qty)
{
this.name = name;
this.author = author;
this.price = price;
this.qty = qty;
}
public string getName()
{
return name;
}
public Author getAuthor()
{
return author;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;

33
}
public int getQty()
{
return qty;
}
public void setQty(int qty)
{
this.qty = qty;
}
public string toString()
{
return $"Book[name = {name},{author.toString()}, price = {price}, qty = {qty}]";
}
}

internal class Program


{
static void Main(string[] args)
{
Console.InputEncoding = Encoding.Unicode;
Console.OutputEncoding = Encoding.Unicode;
Author a = new Author("Phạm Hoàng Gia Khánh","khanhphg@gmail.com", 'm');
Book b = new Book("Kỹ thuật lập trình", a, 200, 9);
Console.WriteLine(b.toString());
Console.ReadKey();
}
}

BUỔI 7
Bài 1
internal class Person
{
private string name;
private int birthYear;
private string address;
public Person() { }
public Person(string name, int birthYear, string address)
{
this.name = name;
this.birthYear = birthYear;
this.address = address;
}
public virtual void Input()
{
Console.Write("Nhap ten : ");
name = Console.ReadLine();
Console.Write("Nhap nam sinh : ");
birthYear = int.Parse(Console.ReadLine());
Console.Write("Nhap dia chi : ");
address = Console.ReadLine();
}
public int GetAge()
{
return DateTime.Now.Year - birthYear;
}
public override string ToString() /*Nếu hàm toString vẫn dùng virtual
{
return $"Person[Name : {name} , Age : {GetAge()}, Address : {address}";
}
}

internal class Student : Person


{
private string program;
private int year;

34
public Student() { }
public Student(string name, int birthYear, string address, string program, int year) : base(name,birthYear,address)
{
this.program = program;
this.year = year;
}
public override void Input()
{
base.Input();
Console.Write("Nhap program : ");
program = Console.ReadLine();
Console.Write("Nhap year : ");
year = int.Parse(Console.ReadLine());
}
public override string ToString()
{
return $"Student[ {base.ToString()} ] - Program : {program}, Year : {year}";
}
public void ChangProgram(string program)
{
this.program = program;
}
}

internal class Staff : Person


{
private string department;
private double salary;
public Staff() { }
public Staff(string name, int birthYear, string address, string department, double salary) : base(name, birthYear, address)
{
this.department = department;
this.salary = salary;
}
public override void Input()
{
Console.Write("Nhap department : ");
department = Console.ReadLine();
Console.Write("Nhap salary : ");
salary = double.Parse(Console.ReadLine());
}
public override string ToString()
{
return $"Staff [ {base.ToString()} ] - Department : {department} , Salary : {salary} ";
}
public void UpDateSalary(double salary)
{
this.salary = salary;
}
}

internal class Program


{
static void Main(string[] args)
{
Student s = new Student();
Staff staff= new Staff();
s.Input();
Console.WriteLine(s.ToString());
staff.Input();
Console.WriteLine(staff.ToString());
Console.ReadKey();
}
}

Bài 2
internal class Fraction
{

35
private int numerator;
private int denominator;
public int Numerator
{
get { return numerator; }
set
{
numerator = value;
}
}
public int Denominator
{
get { return denominator; }
set
{
if (value != 0)
{
denominator = value;
}
else
{
Console.WriteLine("Mau so phai khac 0. " +
"Mau so duoc thiet lap mac dinh la 1");
denominator = 1;
}
}
}
public Fraction()
{
this.numerator = 0;
this.denominator = 1;
}
public Fraction(int numerator, int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
this.Simplify();
}
public Fraction(int numerator)
{
this.numerator = numerator;
this.denominator = 1;
}
private int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
private void Simplify()
{
int uc = GCD(this.numerator, this.denominator);
numerator = numerator / uc;
denominator = denominator / uc;
}
public double Decimal()
{
return (double)numerator / denominator;
}
public Fraction Add(Fraction p)
{
Fraction sum = new Fraction();
sum.Denominator = denominator * p.denominator;
sum.Numerator = numerator * p.denominator + denominator * p.numerator;
sum.Simplify();
return sum;
}
public Fraction Subtract(Fraction p)
{
Fraction tru = new Fraction();

36
tru.Denominator = denominator * p.denominator;
tru.Numerator = numerator * p.denominator - denominator * p.numerator;
tru.Simplify();
return tru;
}
public Fraction Multiply(Fraction p)
{
Fraction nhan = new Fraction();
nhan.Denominator = denominator * p.denominator;
nhan.Numerator = numerator * p.numerator;
nhan.Simplify();
return nhan;
}
public Fraction Divide(Fraction p)
{
Fraction chia = new Fraction();
chia.Denominator = denominator * p.numerator;
chia.Numerator = numerator * p.denominator;
chia.Simplify();
return chia;
}
public override string ToString()
{
return $"{numerator} / {denominator}";
}

internal class MixedFraction : Fraction


{
public MixedFraction(int wholePart,int numerator,int denominator) { }
public MixedFraction(Fraction f): base(f.Numerator,f.Denominator)
{

}
public override string ToString()
{
int ts = this.Numerator;
int ms = this.Denominator;
return ts/ms + "[" +ts%ms + "/"+ ms + "]";
}

internal class Program


{
static void Main(string[] args)
{
Fraction f = new Fraction(7,4);
MixedFraction m = new MixedFraction(f);
Console.WriteLine(m.ToString());
Console.ReadKey();
}
}

BUỔI 8
Bài 1
internal class Calculator
{
public Calculator() { }
public static int Add(int a, int b)
{
return a + b;
}
public static double Add(double a, double b)

37
{
return a + b;
}
public static int Subtract(int a, int b)
{
return a - b;
}
public static double Subtract(double a, double b)
{
return a - b;
}
public static int Multiply(int a, int b)
{
return a * b;
}
public static double Multiply(double a, double b)
{
return a * b;
}
public static int Divide(int a, int b)
{
return a / b;
}
public static double Divide(double a, double b)
{
return a / b;
}
public static int Min(int a, int b)
{
return a < b ? a : b;
}
public static int Min(int a, int b, int c)
{
int min = a;
if (min > b)
min = b;
if (min > c)
min = c;
return min;
}
public static double Min(double a, double b, double c)
{
double min = a;
if (min > b)
min = b;
if (min > c)
min = c;
return min;
}
public static int Max(int a, int b)
{
return a > b ? a : b;
}
public static int Max(int a, int b, int c)
{
int max = a;
if (max < b)
max = b;
if (max < c)
max = c;
return max;
}

public static double Max(double a, double b, double c)


{
double max = a;
if (max < b)
max = b;
if (max < c)
max = c;

38
return max;
}
}

internal class Program


{
static void Main(string[] args)
{
Console.WriteLine(Calculator.Add(4.2, 3.3));
Console.WriteLine(Calculator.Subtract(4, 3));
Console.WriteLine(Calculator.Multiply(4, 3));
Console.WriteLine(Calculator.Divide(4, 3));
Console.WriteLine(Calculator.Min(4, 3,5));
Console.WriteLine(Calculator.Max(1.2,3,4.3));
Console.ReadKey();
}
}

Bài 2
internal class Fraction

{
private int numerator;
private int denominator;
public int Numerator
{
get { return numerator; }
set
{
numerator = value;
}
}
public int Denominator
{
get
{
return denominator;
}
set
{
denominator = value;
}
}
public Fraction() {
numerator = 0;
denominator = 1;
}
public Fraction(int numerator, int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
Simplify();
}
public Fraction(int numerator)
{
this.numerator = numerator;
this.denominator = 1;
}
private int GCD(int a, int b)
{
a = Math.Abs(a);
b = Math.Abs(b);
if (b == 0)
return a;
else
return GCD(b, (a % b));
}
private void Simplify()

39
{

int UCLN = GCD(numerator, denominator);


numerator = numerator / UCLN;
denominator = denominator / UCLN;
}
public override string ToString()
{
return numerator.ToString() + "/" + denominator.ToString();
}
public double ToDecimal()
{
return (1.0 * numerator)/denominator;
}
public Fraction Add(Fraction f)
{

var tuso = numerator * f.denominator + f.numerator * denominator;


var mauso = denominator * f.denominator;
return new Fraction(tuso, mauso);
}
public Fraction Subtract(Fraction f)
{
var tuso = numerator * f.denominator - f.numerator * denominator;
var mauso = denominator * f.denominator;
return new Fraction(tuso, mauso);
}
public Fraction Multiply(Fraction f)
{
var tuso = numerator * f.numerator;
var mauso = denominator * f.denominator;
return new Fraction(tuso, mauso);
}
public Fraction Divide(Fraction f)
{
var tuso = numerator * f.denominator;
var mauso = denominator * f.numerator;
return new Fraction(tuso, mauso);
}
public static Fraction operator -(Fraction a)
{
return new Fraction(-a.numerator, a.denominator);
}
public static Fraction operator +(Fraction a, Fraction b)
{
int ts = a.numerator * b.denominator + b.numerator * a.denominator;
int ms = a.denominator * b.denominator;
return new Fraction(ts, ms);
}

public static Fraction operator -(Fraction a, Fraction b)


{
int ts = a.numerator * b.denominator - b.numerator * a.denominator;
int ms = a.denominator * b.denominator;
return new Fraction(ts, ms);
}
public static Fraction operator *(Fraction a, Fraction b)
{
int ts = a.numerator * b.numerator;
int ms = a.denominator * b.denominator;
return new Fraction(ts, ms);
}
public static Fraction operator /(Fraction a, Fraction b)
{
int ts = a.numerator * b.denominator;
int ms = a.denominator * b.numerator;
return new Fraction(ts, ms);
}
public static bool operator ==(Fraction a, Fraction b)
{

40
return a.numerator == b.numerator && a.denominator == b.denominator;
}
public static bool operator !=(Fraction a, Fraction b)
{
return (!(a == b));
}
public static bool operator >(Fraction a, Fraction b)
{
return a.ToDecimal() > b.ToDecimal();
}
public static bool operator <(Fraction a, Fraction b)
{
return a.ToDecimal() < b.ToDecimal();
}
public static bool operator >=(Fraction a, Fraction b)
{
return (!(a < b));
}
public static bool operator <=(Fraction a, Fraction b)
{
return (!(a > b));
}
}

internal class MixedFraction:Fraction


{
public MixedFraction(int wholePart, int numerator, int denominator):base(denominator*wholePart+numerator,denominator) { }
public MixedFraction(Fraction f):base(f.Numerator,f.Denominator)
{
}
public override string ToString()
{
int ts = this.Numerator;
int ms = this.Denominator;
return ts / ms + "[" + ts % ms + "/" + ms + "]";
}
}

internal class Program


{
static void Main(string[] args)
{
Fraction f1 = new Fraction(7, 4);
Fraction f2 = new Fraction(4, 3);
Console.WriteLine($"{f1.ToString()} + {f2.ToString()} = {(f1 + f2).ToString()}");
Console.WriteLine($"{f1.ToString()} - {f2.ToString()} = {(f1 - f2).ToString()}");
Console.WriteLine($"{f1.ToString()} * {f2.ToString()} = {(f1 * f2).ToString()}");
Console.WriteLine($"{f1.ToString()} / {f2.ToString()} = {(f1 / f2).ToString()}");
bool kt = f1 > f2 ? true: false;
if (kt)
Console.WriteLine($"{f1.ToString()} > {f2.ToString()}");
else
Console.WriteLine($"{f1.ToString()} < {f2.ToString()}");
Console.ReadKey();
}
}

BUỔI 9
Bài 1
abstract class Shape
{
protected string color;
protected bool filled;
public Shape() { }
public Shape(string color, bool filled)

41
{
this.color = color;
this.filled = filled;
}
public string getColor()
{
return color;
}
public void setColor (string color)
{
this.color = color;
}
public bool isFilled()
{
return filled;
}
public void setFilled(bool filled)
{
this.filled = filled;
}
public abstract double getArea();
public abstract double getPerimeter();
public abstract string toString();
}

class Circle:Shape
{
protected double radius;
public Circle() { }
public Circle(double radius)
{
this.radius = radius;
}
public Circle(double radius,string color,bool filled ):base(color,filled)
{
this.radius = radius;
}
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
public override double getArea()
{
return radius*radius*3.14;
}
public override double getPerimeter()
{
return 2*radius*3.14;
}
public override string toString()
{
return $"[Chu vi : {getArea()}] - [Dien tich : {getPerimeter()}]";
}
}

class Rectangle:Shape
{
protected double width;
protected double length;
public Rectangle() { }
public Rectangle(double width, double length)
{
this.width = width;
this.length = length;
}
public Rectangle(double width,double length,string color,bool filled):base(color,filled)

42
{
this.width = width;
this.length = length;
}
public double getWidth()
{
return width;
}
public virtual void setWidth(double width)
{
this.width = width;
}
public double getLength()
{
return length;
}
public virtual void setLength(double length)
{
this.length = length;
}
public override double getArea()
{
return width*length;
}
public override double getPerimeter()
{
return (width+length)*2;
}
public override string toString()
{
return $"[Chu vi : {getPerimeter()}] - [Dien tich : {getArea()}]";
}
}

class Square:Rectangle
{
public Square() { }
public Square(double side) : base(side, side) { }
public Square(double side, string color, bool filled) : base(side, side, color, filled) { }
public double getSide()
{
return getWidth();
}
public void setSide(double side)
{
setWidth(side);
setLength(side);
}
public override void setWidth(double side)
{
setSide(width);
}
public override void setLength(double side)
{
setSide(length);
}
public override string toString()
{
return $"Canh hinh vuong : {getSide()}";
}
}

class Program
{
static void Main(string[] args)
{
Circle circle = new Circle(2);
Console.WriteLine(circle.toString());
Rectangle s= new Rectangle(2,4);
Console.WriteLine(s.toString());

43
Square v= new Square(2);
Console.WriteLine(v.toString());
Console.ReadKey();

}
}

44

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy