You are on page 1of 5

Code:

#include <iostream>
#include <algorithm>

int main() {
int numberTest;
std::cin >> numberTest;

for (int testCase = 1; testCase <= numberTest; ++testCase) {


int n;
std::cin >> n;

int a[] = { 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1 };


int cnt = 0; // dem so luong

std::sort(a, a + 10);

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


cnt += n / a[i];
n %= a[i];
}

std::cout << cnt << std::endl;


}
return 0;
}
Code:
#include <iostream>
#include <vector>
#include <algorithm>

class Item {
public:
int value;
int weight;

Item(int v, int w) : value(v), weight(w) {}


};

// Knapsack class
class Knapsack {
public:
Knapsack(int capacity) : capacity(capacity) {}

int solve(std::vector<Item>& items) {


int numItems = items.size();
std::vector<std::vector<int>> dp(numItems + 1, std::vector<int>(capacity + 1, 0));

for (int i = 1; i <= numItems; ++i) {


for (int currentWeight = 1; currentWeight <= capacity; ++currentWeight) {
if (items[i - 1].weight <= currentWeight) {
dp[i][currentWeight] = std::max(dp[i - 1][currentWeight], dp[i - 1][currentWeight - items[i - 1].weight] +
items[i - 1].value);
}
else {
dp[i][currentWeight] = dp[i - 1][currentWeight];
}
}
}

return dp[numItems][capacity];
}

private:
int capacity;
};

int main() {
int numItems, knapsackCapacity;
std::cout << "nhap so luong muc ";
std::cin >> numItems;

std::vector<Item> items;
std::cout << "nhap gia tri va trong luong cua tung mat hang" << std::endl;
for (int i = 0; i < numItems; ++i) {
int value, weight;
std::cin >> value >> weight;
items.push_back(Item(value, weight));
}

std::cout << "nhap so luong max cua balo: ";


std::cin >> knapsackCapacity;

Knapsack knapsack(knapsackCapacity);
int maxValue = knapsack.solve(items);

std::cout << "gia tri max co the dat duoc lai " << maxValue << std::endl;

return 0;
}
Code:
#include <iostream>
#include <vector>

class DuLich {
private:
int soLuongThanhPho; // Số lượng thành phố
std::vector<std::vector<int>> khoangCach; // Ma trận chi phí di chuyển giữa các thành phố

public:
DuLich(int soLuong, const std::vector<std::vector<int>>& kc) : soLuongThanhPho(soLuong), khoangCach(kc) {}

// Tìm đường đi ngắn nhất bằng thuật toán Greedy


std::vector<int> timDuongDiNganNhat() {
std::vector<int> duongDi;
std::vector<bool> daGheTham(soLuongThanhPho, false); // Đánh dấu các thành phố đã được ghé thăm

// Bắt đầu từ thành phố đầu tiên


int thanhPhoHienTai = 0;
duongDi.push_back(thanhPhoHienTai);
daGheTham[thanhPhoHienTai] = true;

// Lặp qua các thành phố còn lại


for (int i = 1; i < soLuongThanhPho; ++i) {
int thanhPhoTiepTheo = -1;
int khoangCachNhoNhat = INT_MAX;

// Tìm thành phố gần nhất chưa được ghé thăm từ thành phố hiện tại
for (int j = 0; j < soLuongThanhPho; ++j) {
if (!daGheTham[j] && khoangCach[thanhPhoHienTai][j] < khoangCachNhoNhat) {
khoangCachNhoNhat = khoangCach[thanhPhoHienTai][j];
thanhPhoTiepTheo = j;
}
}

// Thêm thành phố gần nhất vào đường đi và đánh dấu đã ghé thăm
duongDi.push_back(thanhPhoTiepTheo);
daGheTham[thanhPhoTiepTheo] = true;
thanhPhoHienTai = thanhPhoTiepTheo;
}

// Quay về thành phố đầu tiên để hoàn thành chu trình


duongDi.push_back(0);

return duongDi;
}
};

int main() {
// Số lượng thành phố và ma trận chi phí di chuyển giữa các thành phố
int soLuongThanhPho = 4;
std::vector<std::vector<int>> khoangCach = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};

// Tạo đối tượng DuLich


DuLich dl(soLuongThanhPho, khoangCach);

// Tìm đường đi ngắn nhất


std::vector<int> duongDiNganNhat = dl.timDuongDiNganNhat();

// In ra đường đi ngắn nhất


std::cout << "Đường đi ngắn nhất: ";
for (int thanhPho : duongDiNganNhat) {
std::cout << thanhPho << " ";
}
std::cout << std::endl;

return 0;
}

You might also like