










Preview text:
BÀI 2: #include using namespace std; // Hàm nhập mảng void Nhap(float a[], int n) {
for (int i = 0; i < n; i++) {
cout << "Nhap a[" << i << "]: "; cin >> a[i]; } } // Hàm in mảng void In(float a[], int n) {
for (int i = 0; i < n; i++) {
cout << a[i] << " "; } cout << endl; }
// Hàm Selection Sort tăng dần
void SelectionSort(float a[], int n) {
for (int i = 0; i < n - 1; i++) { int minIndex = i;
for (int j = i + 1; j < n; j++) { if (a[j] < a[minIndex]) { minIndex = j; } }
// Hoán đổi phần tử nhỏ nhất lên đầu float temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp; } } // Hàm chính int main() { const int n = 10; float a[n];
cout << "Nhap mang " << n << " so thuc:\n"; Nhap(a, n);
cout << "\nMang truoc khi sap xep:\n"; In(a, n); SelectionSort(a, n);
cout << "\nMang sau khi sap xep tang dan:\n"; In(a, n); return 0; }
HÀM Bubble Sort – Tăng dần
void BubbleSortTangDan(float a[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {// giảm thay giấu < float temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } }
Interchange Sort – Tăng dần
void InterchangeSortTangDan(float a[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) { giảm thay dấu< float temp = a[i]; a[i] = a[j]; a[j] = temp; } } } }
Selection Sort – Tăng dần
void SelectionSortTangDan(float a[], int n) {
for (int i = 0; i < n - 1; i++) { int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[minIndex]) { giảm thay dấu< minIndex = j; } } float temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp; } } Bài 1: Tổng S1 #include using namespace std; double S1(int n) { if (n == 1) return 1;
return 1.0 / ((n - 1) * n) + S1(n - 1); } int main() { int n; cout << "Nhap n: "; cin >> n;
cout << "S1 = " << S1(n) << endl; return 0; } Tổng S2 double S2(int n) { if (n == 1) return 1;
return 1.0 / (n * n) + S2(n - 1); } Tổng S3 double S3(int n) { if (n == 1) return 1;
return 1.0 / ((2 * n - 1) * (2 * n - 1)) + S3(n - 1); } Tổng S4 double S4(int n) {
if (n == 1) return 1.0 / (3 * 1 - 1) / (3 * 1 - 1); int x = 3 * n - 1;
return 1.0 / (x * x) + S4(n - 1); } Bài 2 LIFO #include using namespace std; struct Node { int data; Node* next; }; struct Stack { Node* top; };
void initStack(Stack& s) { s.top = nullptr; }
void push(Stack& s, int value) {
Node* newNode = new Node{value, s.top}; s.top = newNode; void printStack(Stack s) { Node* temp = s.top; while (temp) {
cout << temp->data << " "; temp = temp->next; } cout << endl; } int main() { Stack s; initStack(s);
cout << "Nhap 5 so nguyen:\n";
for (int i = 0; i < 5; ++i) { int x; cin >> x; push(s, x); }
cout << "Stack ban dau (LIFO): "; printStack(s); int newValue;
cout << "Nhap phan tu muon them vao dau Stack: "; cin >> newValue; push(s, newValue);
cout << "Stack sau khi them: "; printStack(s); return 0; } FIFO #include using namespace std; struct Node { int info; Node* link; }; struct List { Node* first; Node* last; };
void addLast(List& L, Node* p) { if (L.first == NULL) L.first = L.last = p; else { L.last->link = p; L.last = p; } }
void Nhap5PhanTu(List& L) { L.first = L.last = NULL;
for (int i = 1; i <= 5; i++) { Node* p = new Node;
cout << "Nhap phan tu thu " << i << " : "; cin >> p->info; p->link = NULL; addLast(L, p); } } void ThemL(List& L) { Node* p = new Node;
cout << "\nNhap them phan tu: "; cin >> p->info; p->link = NULL; addLast(L, p); } void InDS(List L) { if (L.first == NULL) {
cout << "Danh Sach Rong!" << endl; } else { Node* p = L.first; while (p != NULL) {
cout << p->info << " "; p = p->link; } cout << endl; } } int main() { List L; Nhap5PhanTu(L);
cout<< "Danh sach day so duoc tao la: " << endl; InDS(L); ThemL(L);
cout << "Danh sach day so sau khi them la: " << endl; InDS(L); return 0; } LIFO #include using namespace std; struct Node { int info; Node* link; }; struct List { Node* first; };
void ThemD(List& L, Node* p) { if (L.first == NULL) L.first = p; else { p->link = L.first; L.first = p; } }
void Nhap5PhanTu(List& L) { L.first = NULL;
for (int i = 1; i <= 5; i++) { Node* p = new Node;
cout << "Nhap phan tu thu " << i << " : "; cin >> p->info; p->link = NULL; ThemD(L, p); } } void ThemL(List& L) { Node* p = new Node;
cout << "\nNhap them phan tu: "; cin >> p->info; p->link = NULL; ThemD(L, p); } void InDS(List L) { if (L.first == NULL) {
cout << "Danh Sach Rong!" << endl; } else { Node* p = L.first; while (p != NULL) {
cout << p->info << " "; p = p->link; } cout << endl; } } int main() { List L; Nhap5PhanTu(L);
cout << "Sau khi nhap 5 phan tu: " << endl; InDS(L); ThemL(L);
cout << "Sau khi them 1 phan tu vao dau: " << endl; InDS(L); return 0; }