-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab1.2.cs
More file actions
166 lines (153 loc) · 4.76 KB
/
Copy pathLab1.2.cs
File metadata and controls
166 lines (153 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
namespace BaiTapTuan1 (2);
class Program1
{
static void main(string[] args)
{
int[] mangSoNguyen = new int[0];
double[] mangSoThuc = new double[0];
while (true)
{
Console.WriteLine("\n=== MENU CHUONG TRINH ===");
Console.WriteLine("1. Tinh tong cac so chan trong mang");
Console.WriteLine("2. Hien thi cac so nguyen to trong mang");
Console.WriteLine("3. Dem so am va so duong trong mang");
Console.WriteLine("4. Tim so lon thu hai trong mang");
Console.WriteLine("5. Hoan vi hai so nguyen");
Console.WriteLine("6. Sap xep mang so thuc tang dan");
Console.WriteLine("0. Thoat");
Console.Write("Chon chuc nang: ");
int luaChon = int.Parse(Console.ReadLine());
switch (luaChon)
{
case 1:
mangSoNguyen = NhapMangNguyen();
Console.WriteLine("Tong cac so chan la: " + TinhTongSoChan(mangSoNguyen));
break;
case 2:
mangSoNguyen = NhapMangNguyen();
HienThiSoNguyenTo(mangSoNguyen);
break;
case 3:
mangSoNguyen = NhapMangNguyen();
DemSoAmVaDuong(mangSoNguyen, out int am, out int duong);
Console.WriteLine($"So am: {am}, So duong: {duong}");
break;
case 4:
mangSoNguyen = NhapMangNguyen();
Console.WriteLine("So lon thu hai la: " + TimSoLonThuHai(mangSoNguyen));
break;
case 5:
Console.Write("Nhap so a: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Nhap so b: ");
int b = int.Parse(Console.ReadLine());
HoanVi(ref a, ref b);
Console.WriteLine($"Sau hoan vi: a = {a}, b = {b}");
break;
case 6:
mangSoThuc = NhapMangThuc();
SapXepTangDan(mangSoThuc);
Console.WriteLine("Mang sau sap xep: " + string.Join(", ", mangSoThuc));
break;
case 0:
Console.WriteLine("Thoat chuong trinh.");
return;
default:
Console.WriteLine("Lua chon khong hop le!");
break;
}
}
}
// ==== Cac ham xu ly ====
static int[] NhapMangNguyen()
{
Console.Write("Nhap so 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 + 1}: ");
arr[i] = int.Parse(Console.ReadLine());
}
return arr;
}
static double[] NhapMangThuc()
{
Console.Write("Nhap so phan tu: ");
int n = int.Parse(Console.ReadLine());
double[] arr = new double[n];
for (int i = 0; i < n; i++)
{
Console.Write($"Nhap phan tu thu {i + 1}: ");
arr[i] = double.Parse(Console.ReadLine());
}
return arr;
}
static int TinhTongSoChan(int[] arr)
{
int tong = 0;
foreach (int x in arr)
{
if (x % 2 == 0)
tong += x;
}
return tong;
}
static bool LaSoNguyenTo(int n)
{
if (n < 2) return false;
for (int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
return false;
}
return true;
}
static void HienThiSoNguyenTo(int[] arr)
{
Console.WriteLine("Cac so nguyen to trong mang:");
for (int i = 0; i < arr.Length; i++)
{
if (LaSoNguyenTo(arr[i]))
Console.WriteLine($"Vi tri {i} -> {arr[i]}");
}
}
static void DemSoAmVaDuong(int[] arr, out int demAm, out int demDuong)
{
demAm = 0;
demDuong = 0;
foreach (int x in arr)
{
if (x < 0) demAm++;
else if (x > 0) demDuong++;
}
}
static int TimSoLonThuHai(int[] arr)
{
int max1 = int.MinValue, max2 = int.MinValue;
foreach (int x in arr)
{
if (x > max1)
{
max2 = max1;
max1 = x;
}
else if (x > max2 && x < max1)
{
max2 = x;
}
}
return max2;
}
static void HoanVi(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
static void SapXepTangDan(double[] arr)
{
Array.Sort(arr);
}
}