You are on page 1of 14

THAM KHẢO BÀI LÀM CỦA BÀI TẬP ÔN 1

Câu 2
#include <iostream>
#include <cmath>

using namespace std;

struct Node {
int a, k;
Node * pNext;
};

struct List {
Node * pHead, * pTail;
};

void createList(List &l) {


l.pHead = NULL;
l.pTail = NULL;
}

Node * createNode(int hs, int mu) {


Node *p = new Node;
if (p) {
p->pNext = NULL;
p->a = hs;
p->k = mu;
}
return p;
}

void addNode(List &l, Node *p) {


if (l.pHead == NULL) {
l.pHead = l.pTail = p;
}
else {
Node * q = NULL;
Node * cur = l.pHead;
while (cur != NULL) {
if (cur->k > p->k) {
q = cur;
cur = cur->pNext;
}
else
break;
}
if (q) {
p->pNext = q->pNext;
q->pNext = p;
if (q == l.pTail) {
l.pTail = p;
}
}
else {
p->pNext = l.pHead;
l.pHead = p;
}
}
}

void Nhap(List &l) {


int m, hs, mu;
cout << "So luong don thuc: ";
cin >> m;
while (m) {
cout << "he so: ";
cin >> hs;
cout << "so mu: ";
cin >> mu;
Node *p = createNode(hs, mu);
addNode(l, p);
m--;
}
}

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;
}

while (l.pHead != NULL) {


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;
}
}

double sumList(List l, double x) {


double kq = 0;
while (l.pHead) {
kq += pow(x, l.pHead->k) * l.pHead->a;
l.pHead = l.pHead->pNext;
}
return kq;
}

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>

using namespace std;

struct TreeNode {
int Maso;
string Hoten;
double DiemTB;
TreeNode *pLeft, *pRight;
};

typedef TreeNode * TREE;

TreeNode * createTreeNode(int ms, string ht, double tb) {


TreeNode * p = new TreeNode;
if (p) {
p->Maso = ms;
p->Hoten = ht;
p->DiemTB = tb;
p->pLeft = p->pRight = NULL;
}
return p;
}

void addTreeNode(TREE &root, TreeNode *p) {


if (root == NULL) {
root = p;
return;
}
if (root->Maso == p->Maso)
return;

if (root->Maso > p->Maso)


addTreeNode(root->pLeft, p);
else
addTreeNode(root->pRight, p);
}

void printHocsinh(TreeNode * p) {
cout << "Ma so: " << p->Maso << endl;
cout << "Ho ten: " << p->Hoten << endl;
cout << "Diem trung binh: " << p->DiemTB << endl;
}

void nhapHocsinh(TREE &root) {


int ms;
string ht;
double tb;

cout << "Nhap danh sach hoc sinh\n";


cout << "Ma so: ";
cin >> ms; cin.ignore();
while (ms > 0) {
cout << "Ho ten: ";
getline(cin, ht);
cout << "Diem trung binh: ";
cin >> tb; cin.ignore();
cout << "=======================\n";
TreeNode * p = createTreeNode(ms, ht, tb);
if (p == NULL) return;
addTreeNode(root, p);
cout << "Ma so: ";
cin >> ms; cin.ignore();
}
}
void printDanhsach(TREE root) {
if (root == NULL) return;

printHocsinh(root);
printDanhsach(root->pLeft);
printDanhsach(root->pRight);
}

void printByTBAB(TREE root, int a, int b) {


if (root == NULL) return;
if (root->DiemTB >= a && root->DiemTB <= b)
printHocsinh(root);
printByTBAB(root->pLeft, a, b);
printByTBAB(root->pRight, a, b);
}

int countByTB5(TREE root) {


if (root == NULL) return 0;

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>

using namespace std;

/**
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;
}

int h(int k, int i) {


return (h(k) + i) % m;
}

struct Mathang {
int Mahang;
string Tenhang;
int Giaban;
};

Mathang bang[MAX];

void createBangbam(int kichthuoc) {


for (int i = 0; i < kichthuoc; i++) {
bang[i].Mahang = EMPTY;
}
m = kichthuoc;
}

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
}

int xoaMathang(int mahang) {


int index = h(mahang);
int i = 1;

while (bang[index].Mahang != EMPTY && i < m-1) {


if (bang[index].Mahang == mahang) {
bang[index].Mahang = DELETED;
return 1; // xoa duoc
}
index = h(mahang, i);
i++;
}

return 0; // khong tim thay phan tu can xoa


}

int timMathang(int mahang) {


int index = h(mahang);
int i = 1;
while (bang[index].Mahang != EMPTY && i < m-1) {

if (bang[index].Mahang == mahang)
return index; // tim thay

index = h(mahang, i);


i++;
}

return -1; // khong tim thay


}

void nhapMathang(Mathang &p) {


cout << "Ma hang: ";
cin >> p.Mahang; cin.ignore();
cout << "Ten hang: ";
getline(cin, p.Tenhang);
cout << "Gia ban: ";
cin >> p.Giaban; cin.ignore();
cout << "=========================\n";
}

void nhap() {
int n;
Mathang p;
cout << "Nhap so luong mat hang: ";
cin >> n; cin.ignore();

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


nhapMathang(p);
if (!themMathang(p))
cout << "Khong them duoc!" << endl;
}
}
void printBang() {
for (int i = 0; i < m; i++) {
if (bang[i].Mahang > 0) {
cout << i << '\t' << bang[i].Mahang << endl;
continue;
}
if (bang[i].Mahang == EMPTY) {
cout << i << "\tEMPTY" << endl;
}
else {
cout << i << "\tDELETED" << endl;
}
}
}

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>

using namespace std;


struct DinhKe {
int dinh, trongso;
};

struct CheckPoint {
int dinh, dodai;
};

void printDSKe(vector<vector<DinhKe>> ds, vector<string> tdinh) {


for (int i = 0; i < ds.size(); i++) {
cout << tdinh[i] << " --> ";
for (int j = 0; j < ds[i].size(); j++) {
cout << tdinh[ds[i][j].dinh] << '(' << ds[i][j].trongso << ") ";
}
cout << endl;
}
}

void nhapDothi(map<string, int> &chiso,


vector<string> &tendinh,
vector<vector<DinhKe>> &danhsachke) {

int N, E, M, ts;
DinhKe dke;
vector<DinhKe> dsdinh;
string dau, cuoi;

cout << "Nhap so dinh: ";


cin >> N; cin.ignore();
cout << "Nhap so canh: ";
cin >> E; cin.ignore();
cout << "Nhap danh sach dinh: " << endl;
for (int i = 0; i < N; i++) {
cin >> dau;
chiso[dau] = i;
tendinh.push_back(dau);
danhsachke.push_back(dsdinh);
}
cout << "Nhap danh sach canh theo dang DAU CUOI TRONGSO: " << endl;
for (int i = 0; i < E; i++) {
cin >> dau >> cuoi >> ts;
dke.dinh = chiso[cuoi];
dke.trongso = ts;
danhsachke[chiso[dau]].push_back(dke);
}
}

int check(vector<CheckPoint> path, int dinh) {


for (int i = 0; i < path.size(); i++) {
if (path[i].dinh == dinh)
return true;
}
return false;
}

void DFS(vector<vector<DinhKe>> danhsachke, vector<string> tendinh, int dau, int cuoi) {


stack<vector<CheckPoint>> open;
CheckPoint cp, newcp;
vector<CheckPoint> path, newpath;
cp.dinh = dau;
cp.dodai = 0;
path.push_back(cp);
open.push(path);
while (!open.empty()) {
path = open.top(); open.pop();
cp = path[path.size()-1];
for (int i = 0; i < danhsachke[cp.dinh].size(); i++) {
if (danhsachke[cp.dinh][i].dinh == cuoi) {
int tongtrongso = cp.dodai + danhsachke[cp.dinh][i].trongso;
cout << tendinh[path[0].dinh];
for (int j = 1; j < path.size(); j++) {
cout << "->" << tendinh[path[j].dinh];
}
cout << " tong trong so: " << tongtrongso << endl;
continue;
}
if (!check(path, danhsachke[cp.dinh][i].dinh)) {
newpath = path;
newcp.dinh = danhsachke[cp.dinh][i].dinh;
newcp.dodai = cp.dodai + danhsachke[cp.dinh][i].trongso;
newpath.push_back(newcp);
open.push(newpath);
}
}
}
}

void BFS(vector<vector<DinhKe>> danhsachke, vector<string> tendinh, int dau, int cuoi) {


queue<vector<CheckPoint>> open;
CheckPoint cp, newcp;
vector<CheckPoint> path, newpath;
cp.dinh = dau;
cp.dodai = 0;
path.push_back(cp);
open.push(path);
while (!open.empty()) {
path = open.front(); open.pop();
cp = path[path.size()-1];
for (int i = 0; i < danhsachke[cp.dinh].size(); i++) {
if (danhsachke[cp.dinh][i].dinh == cuoi) {
int tongtrongso = cp.dodai + danhsachke[cp.dinh][i].trongso;
cout << tendinh[path[0].dinh];
for (int j = 1; j < path.size(); j++) {
cout << "->" << tendinh[path[j].dinh];
}
cout << " tong trong so: " << tongtrongso << endl;
continue;
}
if (!check(path, danhsachke[cp.dinh][i].dinh)) {
newpath = path;
newcp.dinh = danhsachke[cp.dinh][i].dinh;
newcp.dodai = cp.dodai + danhsachke[cp.dinh][i].trongso;
newpath.push_back(newcp);
open.push(newpath);
}
}
}
}

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;
}

You might also like