Professional Documents
Culture Documents
Câu 2
#include <iostream>
#include <cmath>
struct Node {
int a, k;
Node * pNext;
};
struct List {
Node * pHead, * pTail;
};
void printList(List l) {
if (l.pHead) {
if (l.pHead->a > 1)
cout << l.pHead->a;
else
if (l.pHead->a == -1)
cout << "-";
else
if (l.pHead->a < -1)
cout << l.pHead->a;
if (l.pHead->k > 1)
cout << "x^" << l.pHead->k;
else
if (l.pHead->k == 1)
cout << "x";
l.pHead = l.pHead->pNext;
}
if (l.pHead->k > 1)
cout << "x^" << l.pHead->k;
else
if (l.pHead->k == 1)
cout << "x";
l.pHead = l.pHead->pNext;
}
}
int main() {
List l;
createList(l);
Nhap(l);
printList(l);
cout << endl;
double x;
cout << "Tinh gia tri da thuc voi x = ";
cin >> x;
cout << "Gia tri tinh duoc: " << sumList(l, x);
return 0;
}
Câu 3
#include <iostream>
#include <string>
struct TreeNode {
int Maso;
string Hoten;
double DiemTB;
TreeNode *pLeft, *pRight;
};
void printHocsinh(TreeNode * p) {
cout << "Ma so: " << p->Maso << endl;
cout << "Ho ten: " << p->Hoten << endl;
cout << "Diem trung binh: " << p->DiemTB << endl;
}
printHocsinh(root);
printDanhsach(root->pLeft);
printDanhsach(root->pRight);
}
if (root->DiemTB < 5)
return 1 + countByTB5(root->pLeft) + countByTB5(root->pRight);
else
return countByTB5(root->pLeft) + countByTB5(root->pRight);
int main() {
TREE root = NULL;
int a, b;
nhapHocsinh(root);
printDanhsach();
cout << "Nhap khoang diem trung binh [a, b]" << endl;
cout << "a = ";
cin >> a; cin.ignore();
cout << "b = ";
cin >> b; cin.ignore();
printByTBAB(root, a, b);
cout << "So hoc sinh co diem duoi 5: " << countByTB5(root) << endl;
return 0;
}
Câu 4
#include <iostream>
#include <string>
/**
Kich thuoc bang bam m = 23 (nguyen to lon hon so luong phan tu 15)
Ham bam: h(k) = k mod 23
Ham bam lai: h(k, i) = (h(k) + i) mod 23 (do khong yeu cau tham do theo ham nao nen co the chon ham
tham do tuyen tinh)
**/
#define MAX 100
#define EMPTY -1
#define DELETED -2
int m = 0;
int h(int k) {
return k % m;
}
struct Mathang {
int Mahang;
string Tenhang;
int Giaban;
};
Mathang bang[MAX];
int themMathang(Mathang p) {
int index = h(p.Mahang);
int i = 1;
while (bang[index].Mahang != EMPTY && i < m-1) {
index = h(p.Mahang, i);
i++;
}
if (i < m-1) {
bang[index] = p;
return 1; // them duoc
}
return 0; // bang bam day
}
if (bang[index].Mahang == mahang)
return index; // tim thay
void nhap() {
int n;
Mathang p;
cout << "Nhap so luong mat hang: ";
cin >> n; cin.ignore();
void xoa() {
int mahang;
cout << "Nhap ma hang can xoa (0 de thoat): ";
cin >> mahang; cin.ignore();
while (mahang > 0) {
if (!xoaMathang(mahang)) {
cout << "Khong xoa duoc" << endl;
}
printBang();
cout << "Nhap ma hang can xoa (0 de thoat): ";
cin >> mahang; cin.ignore();
}
}
void tim() {
int mahang, index;
cout << "Nhap ma hang can tim (0 de thoat): ";
cin >> mahang; cin.ignore();
while (mahang > 0) {
index = timMathang(mahang);
if (index < 0) {
cout << "Khong tim thay" << endl;
}
else {
cout << "Ma hang o vi tri " << index << endl;
}
printBang();
cout << "Nhap ma hang can tim (0 de thoat): ";
cin >> mahang; cin.ignore();
}
}
int main() {
createBangbam(23);
nhap();
xoa();
tim();
return 0;
}
Câu 5
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
struct CheckPoint {
int dinh, dodai;
};
int N, E, M, ts;
DinhKe dke;
vector<DinhKe> dsdinh;
string dau, cuoi;
int main() {
map<string, int> ChiSo;
vector<string> TenDinh;
vector<vector<DinhKe>> DSKe;
string dau, cuoi;
nhapDothi(ChiSo, TenDinh, DSKe);
printDSKe(DSKe, TenDinh);
cout << "Tim tat ca duong di tu A den B\n";
cout << "A = ";
cin >> dau;
cout << "B = ";
cin >> cuoi;
BFS(DSKe, TenDinh, ChiSo[dau], ChiSo[cuoi]);
DFS(DSKe, TenDinh, ChiSo[dau], ChiSo[cuoi]);
return 0;
}