Thá C Hã NH Buá - I 7

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

Tên: Dương Tấn Đạt

Lớp:TH2129
MSSV: 21dh113556
Bai1
Static void NhapMang(out int[] a)
{
Console.Write("n = ");
int n = int.Parse(Console.ReadLine());
a = new int[n];

for (int i = 0; i < n; i++)


{
Console.Write($"a[{i}] = ");
a[i] = int.Parse(Console.ReadLine());
}
}

static void XuatMang(int[] a)


{
Console.Write("Gia tri cua mang la : ");
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
Console.WriteLine();
}

static void InterchangeSort(int[] a)


{
for (int i = 0; i < a.Length; i++)
{
for (int j = i + 1; j < a.Length; j++)
if (a[i] > a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

static void BubbleSort(int[] a)


{
for (int i = 0; i < a.Length - 1; ++i)
{
for (int j = 0; j < a.Length - i - 1; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}

static void Main(string[] agrs)


{
int[] a;
NhapMang(out a);
BubbleSort(a);
XuatMang(a);
Console.ReadKey();
Bài:2
static void NhapMang(out int[] a)
{
Console.Write("n = ");
int n = int.Parse(Console.ReadLine());
a = new int[n];

for (int i = 0; i < n; i++)


{
Console.Write($"a[{i}] = ");
a[i] = int.Parse(Console.ReadLine());
}
}

static void XuatMang(int[] a)


{
Console.Write("Gia tri cua mang la : ");
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
Console.WriteLine();
}

static void interchangesort(int[] a)


{
for (int i = 0; i < a.Length; i++)
{
for (int j = i + 1; j < a.Length; j++)
if (a[i] > a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

//static void BubbleSort(int[] a)


//{
// for (int i = 0; i < a.Length - 1; ++i)
// {
// for (int j = 0; j < a.Length - i - 1; j++)
// {
// if (a[j] > a[j + 1])
// {
// int temp = a[j];
// a[j] = a[j + 1];
// a[j + 1] = temp;
// }
// }
// }
//}

static void Main(string[] agrs)


{
int[] a;
NhapMang(out a);
interchangesort(a);
//BubbleSort(a);
XuatMang(a);
}
}
}
Console.ReadKey();
Bài3
static void NhapMang(out int[] a)
{
Console.Write("n = ");
int n = int.Parse(Console.ReadLine());
a = new int[n];

for (int i = 0; i < n; i++)


{
Console.Write($"a[{i}] = ");
a[i] = int.Parse(Console.ReadLine());
}
}

static void XuatMang(int[] a)


{
Console.Write("Gia tri cua mang la : ");
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
Console.WriteLine();
}

static void InterchangeSort(int[] a)


{
for (int i = 0; i < a.Length; i++)
{
for (int j = i + 1; j < a.Length; j++)
if (a[i] > a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

static void TimK(int[] a)


{
Console.Write("k = ");
int k = int.Parse(Console.ReadLine());
if (k < 1 || k > a.Length)
{
Console.WriteLine("So k khong phu hop");
}
else
{
InterchangeSort(a);
Console.WriteLine($"Phan tu lon thu {k} la :
{a[a.Length - k]}");
}
}

static void Main(string[] agrs)


{
int[] a;
NhapMang(out a);
XuatMang(a);
TimK(a);
Console.ReadKey();

Bài5
static void NhapMang(out int[] a)
{
Console.Write("n = ");
int n = int.Parse(Console.ReadLine());
a = new int[n];

for (int i = 0; i < n; i++)


{
Console.Write($"a[{i}] = ");
a[i] = int.Parse(Console.ReadLine());
}
}

static void XuatMang(int[] a)


{
Console.Write("Gia tri cua mang la : ");
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
Console.WriteLine();
}

static void InterchangeSort(int[] a)


{
for (int i = 0; i < a.Length; i++)
{
for (int j = i + 1; j < a.Length; j++)
if (a[i] > a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

static void DemPhanTu(int[] a)


{
InterchangeSort(a);
int count = 1;
for (int i = 1; i < a.Length; i++)
{
if (a[i] > a[i - 1])
{
count++;
}
}
Console.WriteLine($"So phan tu khac nhau trong
mang la: {count}");
}

static void Main(string[] agrs)


{
int[] a;
NhapMang(out a);
XuatMang(a);
DemPhanTu(a);
Console.ReadKey();
Bai6
static void NhapMang(out int[] a)
{
Console.Write("n = ");
int n = int.Parse(Console.ReadLine());
a = new int[n];

for (int i = 0; i < n; i++)


{
Console.Write($"a[{i}] = ");
a[i] = int.Parse(Console.ReadLine());
}
}

static void XuatMang(int[] a)


{
Console.Write("Gia tri cua mang la : ");
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
Console.WriteLine();
}

static void InterchangeSort(int[] a)


{
for (int i = 0; i < a.Length; i++)
{
for (int j = i + 1; j < a.Length; j++)
if (a[i] > a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
static void DemPhanTu(int[] a)
{
InterchangeSort(a);
int count = 1;
for (int i = 1; i < a.Length; i++)
{
if (a[i] > a[i - 1])
{
count++;
}
}
Console.WriteLine($"So phan tu khac nhau trong
mang la: {count}");
}

static void Main(string[] agrs)


{
int[] a;
NhapMang(out a);
XuatMang(a);
DemPhanTu(a);
Console.ReadKey();
Bai7
static void NhapMang(out int[] a)
{
Console.Write("n = ");
int n = int.Parse(Console.ReadLine());
a = new int[n];

for (int i = 0; i < n; i++)


{
Console.Write($"a[{i}] = ");
a[i] = int.Parse(Console.ReadLine());
}
}

static void XuatMang(int[] a)


{
Console.Write("Gia tri cua mang la : ");
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] + " ");
}
Console.WriteLine();
}

static bool IsNT(int n)


{
if (n < 2)
{
return false;
}
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}

static void TimSoNT(int[] a)


{
Console.Write("Cac so nguyen to trong mang: ");
for (int i = 0; i < a.Length; i++)
{
if (IsNT(a[i]))
{
Console.Write(a[i] + " ");
}
}
Console.WriteLine();
}
static void Main(string[] agrs)
{
int[] a;
NhapMang(out a);
XuatMang(a);
TimSoNT(a);
Console.ReadKey();

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