0% found this document useful (0 votes)
96 views77 pages

1. Tìm kiếm và sắp xếp: // in phuong an xep hau

The document contains code snippets related to algorithms and data structures in C++ including: 1) Searching and sorting arrays using find, binary_search, lower_bound, and upper_bound functions. 2) Dynamic programming to find the longest common subsequence between two arrays. 3) Floyd-Warshall algorithm to find shortest paths in a weighted graph. 4) Backtracking algorithm to solve the knapsack problem and print the optimal solution. 5) Bubble sort algorithm implementations to sort arrays. 6) Code to find the longest common subsequence between two arrays.

Uploaded by

Pouk Singlattana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views77 pages

1. Tìm kiếm và sắp xếp: // in phuong an xep hau

The document contains code snippets related to algorithms and data structures in C++ including: 1) Searching and sorting arrays using find, binary_search, lower_bound, and upper_bound functions. 2) Dynamic programming to find the longest common subsequence between two arrays. 3) Floyd-Warshall algorithm to find shortest paths in a weighted graph. 4) Backtracking algorithm to solve the knapsack problem and print the optimal solution. 5) Bubble sort algorithm implementations to sort arrays. 6) Code to find the longest common subsequence between two arrays.

Uploaded by

Pouk Singlattana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 77

1.

Tìm kiếm và sắp xếp


#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n = 9;
int a[] = { 1, 2, 2, 3, 6, 6, 6, 7, 8};
cout << "find(6): " << find(a+0, a+n, 6) - a << endl;
cout << "binary_search(5): " << binary_search(a+0, a+n, 5) << endl;
cout << "binary_search(6): " << binary_search(a+0, a+n, 6) << endl;
cout << "lower_bound(5): " << lower_bound(a+0, a+n, 5) - a << endl;
cout << "lower_bound(6): " << lower_bound(a+0, a+n, 6) - a << endl;
cout << "upper_bound(5): " << upper_bound(a+0, a+n, 5) - a << endl;
cout << "upper_bound(6): " << upper_bound(a+0, a+n, 6) - a << endl;
}

// đệ quy, quay lui, nhánh cận


#include <iostream>
using namespace std;
const int MAX = 100;
int a[MAX], n = 20, dem = 0;

// in phuong an xep hau


void print() {
dem++;
cout << "Phuong an thu " << dem << ":" << endl;
for (int i = 1; i <= n; i++, cout << endl)
for (int j = 1; j <= n; j++)
if (j == a[i]) cout << " x";
else cout << " .";
}

1
bool check(int h, int c) {

// kiem tra cot


for (int i = 1; i < h; i++)
if (c == a[i]) return false;

// kiem tra duong cheo chinh


for (int i = 1; i < h; i++)
if (c-h == a[i]-i) return false;

// kiem tra duong cheo chinh


for (int i = 1; i < h; i++)
if (c+h == a[i]+i) return false;
return true;
}

// dat quan hau o dong thu k


void dat(int k) {
if (k > n) {
print(); return;
}
for (int i = 1; i <= n; i++)
if (check(k, i)) {
a[k] = i; dat(k+1);
}
}
int main() {
dat(1);
}
</iostream>

2
2. //Quy hoạch động
#include <iostream>
using namespace std;
const int MAX = 200;
int a[] = { 0, 3, 1, 2, 0, 4, 3 };
int b[] = { 0, 1, 2, 3, 4, 3, 2, 1 };
int m = 6, n = 7;
int kq[MAX][MAX];
int S(int p, int q) {
if (p == 0 || q == 0) return 0;
if (kq[p][q] == -1)
if (a[p] == b[q]) kq[p][q] = S(p-1, q-1) + 1;
else kq[p][q] = max(S(p-1, q), S(p, q-1));
return kq[p][q];
}
int bottom_up(int p, int q) {
for (int i = 0; i < MAX; i++) kq[0][i] = kq[i][0] = 0;
for (int i = 1; i <= p; i++) {
for (int j = 1; j <= q; j++)
if (a[i] == b[j]) kq[i][j] = kq[i-1][j-1] + 1;
else kq[i][j] = max(kq[i-1][j], kq[i][j-1]);
}
return kq[p][q];
}
int main() {
for (int i = 0; i < MAX; i++)
for (int j = 0; j < MAX; j++) kq[i][j] = -1;
cout << "De quy co nho: " << S(m, n) << endl;
cout << "Bottom-up: " << bottom_up(m, n);
}

3
</iostream>

3. //Đồ Thị

#include <iostream>
#include <queue>
using namespace std;
const int MAX = 100;
const int INF = 100000;
int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];
int n, m, u, v, d, p, q;
void nhap_du_lieu() {
cin >> n >> m;
for (int i = 0; i < n; a[i][i] = 0, i++)
for (int j = 0; j < n; j++)
a[i][j] = INF;
for (int i = 0; i < m; i++) {
cin >> u >> v >> d;
a[u][v] = a[v][u] = d;
}
cin >> p >> q;
}
void floyd_warshall() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
b[i][j] = a[i][j], c[i][j] = j;
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (b[i][j] > b[i][k] + b[k][j]) {
b[i][j] = b[i][k] + b[k][j];

4
c[i][j] = c[i][k];
}
if (b[p][q] == INF) cout << "Khong ton tai duong di";
else {
cout << "Duong di ngan nhat: " << b[p][q] << endl;
while (p != q) {
cout << p << " --> ";
p = c[p][q];
}
cout << q;
}
}

int main() {
nhap_du_lieu();
floyd_warshall();
}

/*
Test 1:
6
10
015
022
122
134
147
2 3 20
246
342

5
351
455
05

4. ĐỀ BÀI in tổng các số (zoro)


Zoro là một học sinh tiểu học. Cậu ta thích giải toán. Một ngày nọ, Zoro thử giải một bài toán.
Nhưng cậu ta không thể giải quyết nó một cách hiệu quả bởi vì mới học tiểu học. Vì bạn là một
lập trình viên, nên cậu ta muốn nhờ bạn giúp đỡ bài toán này. Và vấn đề ở đây là tính tổng của các
số liên tiếp.
Ví dụ: Nếu số thứ nhất là 2 và số tiếp theo là 5, thì kết quả sẽ là: 2 + 3 + 4 + 5 = 14.
Đầu vào
Mỗi dòng chứa 2 số a và b. Đầu vào kết thúc bởi kí tự kết thúc file.
Đầu ra
In ra tổng của các số liên tiếp giữa a và b (bao gồm cả 2 số đó).
Ràng buộc
-10^8 ≤ (a , b) ≤ 10^8
Ví dụ
Input:
25
5 10
Output:
14
45
#include <iostream>
using namespace std;
long long sumAll(long long a, long long b) {
long long n;

if (b > a) n = b - a + 1;
else n = a - b + 1;

6
return n * (a + b) / 2;
}

int main() {
ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);

int a, b;
while(cin >> a >> b) {
cout << sumAll(a, b) << endl;
}

return 0;
}

}
5. Bài toán cái túi
#include <iostream>

using namespace std;

const int max_arr = 1000;

int n, T, maxP = 0, count = 0;

int A[max_arr], a[max_arr],


w[5] = {1, 10, 5, 2, 6}, p[5] = {6, 3, 28, 13, 9};

bool cd[max_arr];

7
void inkq(int k){ // k: vi tri
int TP = 0;
for(int i = 0; i <= k; i++){
TP += p[a[i]];
}

if(maxP < TP){


maxP = TP;
for(int i = 0; i <= k; i++){
A[i] = a[i];
}
count = k;
}
}

void Try(int k, int tong){


bool chon = false;

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


if((cd[i]) && (tong + w[i] <=T)){
a[k] = i;
cd[i] = false;
if(k == n) {
inkq(k);
} else Try(k + 1, tong + w[i]);
cd[i] = true;
chon = true;
}
if(!chon) inkq(k - 1);
}

8
int main(){

// cout<<"Nhap n: "; cin>>n;


// cout<<"Nhap T: "; cin>>T;

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


// cout<<"Nhap w["<<i + 1<<"]= "; cin>>w[i];
// }
// for(int i = 0; i < n; i++){
// cout<<"Nhap p["<<i + 1<<"]= "; cin>>p[i];
// }

n = 5, T = 11;

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


cd[i] = true;
}

Try(0, 0);
cout<<maxP<<endl;

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


cout<<A[i] + 1<<" ";
}
}

6. Bubble_ct1
#include<iostream>
using namespace std;
void bubbleSort(int arr[], int n){

9
bool nt = false;
for(int i = 0; i < n - 1; i++){
for(int j = n - 1; j > i; j--){
if (arr[j] < arr[j - 1]){
int tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
nt = true;
}
}
if(!nt)
break;
}
}

void printArray(int arr[], int n){


for(int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main(){
cout << "Nhap n: ";
int n; cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
bubbleSort(arr, n);
cout<<"Day sau sap xep: ";
printArray(arr, n);
return 0;

10
}

7. Bubble_ct2
#include<iostream>
using namespace std;

void bubbleSort(int a[], int n){


bool swapped = true;
int start = 0;
int _end = n - 1;
while(swapped){
swapped = false;
for (int i = start; i < _end; i++){
if(a[i] > a[i + 1]){
swap(a[i], a[i + 1]);
swapped = true;
}
}
if (!swapped)
break;
swapped = false;
--_end;
for(int i = _end - 1; i >= start; --i){
if(a[i] > a[i + 1]){
swapped = true;
swap(a[i], a[i + 1]);
}
}
++start;
}
}

11
int main(){
int n;
cout<<"Nhap n:";
cin >> n;
int a[n];
for(int i = 0; i< n; i++)
cin >> a[i];
bubbleSort(a, n);
for(int i = 0;i < n; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}

8. Dãy con chung


#include <iostream>
#include <math.h>
#include<vector>
#include<algorithm>
using namespace std;

int n,m;
int A[1003];
int B[1003];
int C[1003][1003];

void Nhap()
{
cout<<" Nhap so phan tu cua day n:";
cin>>n;

12
for (int i=1; i<=n; i++)
cin>>A[i];
cout<<" Nhap so phan tu cua day m:";
cin>>m;
for (int i=1; i<=m; i++)
cin>>B[i];

void Timdayconchung()
{
for(int i=1; i <=m; i++){
for(int j =1; j <=n; j++){

if(A[i] == B[j]){
C[i][j] = C[i-1][j-1] +1;
}else{
C[i][j] = max(C[i-1][j],C[i][j-1]);
}
}
}
for(int i=0; i <=m; i++){
for(int j = 0; j <=n; j++){
cout<< C[i][j];
}
cout<<endl;
}
}

int main ()

13
{
Nhap();
Timdayconchung();
cout<<"so pt chung "<< C[m][n] <<endl;

cout<<"day chung dai nhat: ";


vector<int> result;

while(m >= 1 && n >= 1){


while(C[m][n] == C[m][n-1]){
n--;
}
if (B[n] != 0)
result.push_back(B[n]);
m--;
n--;
}
reverse(result.begin(), result.end());
for(int i = 0; i < result.size(); i++)
cout << result[i] << " ";
return 0;
}

9. Hoan vi

#include<iostream>
using namespace std;

int cd[100];
int a[100];
int n;

14
void output()
{
for (int i =1; i<= n; i++)
cout<<a[i]<<" ";
cout<<endl;
}

void Hoan_Vi(int k){


for(int i =1; i<= n; i++){
if(!cd[i]){ //kiem tra phan tu chua duoc chon danh dau
a[k] = i; //luu mot phan tu vao hoan vi
cd[i] = true;
if(k == n)
output();
else
Hoan_Vi(k+1);
cd[i] = false;
}
}
}

int main(){
// int n;
cout<<" nhap n:";
cin>>n;
Hoan_Vi(true);

return 0;
}

15
10. Kt (độ dài đường dài)
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int n,m,s,t,p,q;
int A[10][10];
//int ci[]={0,1,0,0};
//int cj[]={0,0,1,-1};

int ci[] = { 0,-1,0,1 };


int cj[] = { -1,0,1,0 };

bool K[10][10];
int d=0;

void input(){
freopen("hai.inp","r",stdin);
cin>>n>>m>>s>>t>>p>>q;
for(int i=0;i<=n+1;i++){
for(int j=0;j<=m+1;j++){
A[0][j]=0;
A[i][0]=0;
A[i][m+1]=0;
A[n+1][j]=0;
K[0][j]=false;
K[i][0]=false;
K[i][m+1]=false;
K[n+1][j]=false;

16
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>A[i][j];
K[i][j]=true;
}
}
}
void output(){
for(int i=0;i<=n+1;i++){
for(int j=0;j<=m+1;j++){
cout<<A[i][j]<<" ";
}
cout<<endl;
}
}
void dixuong(){
for(int i=0;i<=4;i++){
if(A[s+ci[i]][t+cj[i]]==A[s][t] && K[s+ci[i]][t+cj[i]]){
K[s+ci[i]][t+cj[i]]=false;
s=s+ci[i];
t=t+cj[i];
d=d+1;
cout<<s<<" "<<t<<endl;
if(s==p && t==q){
cout<<"tim thay ket qua"<<endl;
break;
}
dixuong();

17
}
}
}
int main(){
input();
output();
dixuong();
cout<<" do dai duong di: "<<d<<endl;
return 0;

11. LCA
#include <iostream>
#include <math.h>
#include<vector>
#include<algorithm>
using namespace std;

int n,m;
int A[1003];
int B[1003];
int C[1003][1003];

void Nhap()
{
cin>>n;
for (int i=1; i<=n; i++)
cin>>A[i];
cin>>m;

18
for (int i=1; i<=m; i++)
cin>>B[i];

void LCS()
{
for(int i=1; i <=m; i++){
for(int j =1; j <=n; j++){

if(A[i] == B[j]){
C[i][j] = C[i-1][j-1] +1;
}else{
C[i][j] = max(C[i-1][j],C[i][j-1]);
}
}
}
for(int i=0; i <=m; i++){
for(int j = 0; j <=n; j++){
cout<< C[i][j];
}
cout<<endl;
}
}

int main ()
{
Nhap();
LCS();
cout<<"so pt chung "<< C[m][n] <<endl;

19
cout<<"day chung dai nhat: ";
vector<int> result;

while(m >= 1 && n >= 1){


while(C[m][n] == C[m][n-1]){
n--;
}

if (B[n] != 0)
result.push_back(B[n]);
m--;
n--;
}
reverse(result.begin(), result.end());
for(int i = 0; i < result.size(); i++)
cout << result[i] << " ";
return 0;
}

12. Main
#include <bits/stdc++.h>
using namespace std;

struct TIM {
int x, y;
TIM(int a = 0, int b = 0) {
x = a;
y = b;
}
};

20
int arr[1000][1000];
bool check[1000][1000];
int n, m, a = 0, b = 0;

int ch[] = { 0,-1,0,1 };


int cc1[] = { -1,0,1,0 };
TIM A[1000];
TIM B[1000];

void init(){
freopen("dd.txt", "r", stdin);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for(int j =1; j <= m; j++) {
cin>> arr[i][j];
check[i][j] = true;
}
}
}

void bfs(int h,int c, int value) {


for (int i = 0; i < 4; i++) {
if (arr[h + ch[i]][c + cc1[i]] == value && check[h + ch[i]][c + cc1[i]] == true) {
b++;
B[b] = TIM(h + ch[i], c +cc1[i] );
check[h + ch[i]][c + cc1[i]] = false;
bfs(h + ch[i], c + cc1[i], arr[h + ch[i]][c + cc1[i]]);
}
}

21
}

void solve(){
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
check[i][j] = false;
B[b] = TIM(i, j);
bfs(i, j, arr[i][j]);
if (a <= b) {
for (int k = 0; k <= b; k++) {
A[k] = B[k];
}
a = b;
b = 0;
}
}
}

cout << a+1 << endl;


for (int i = 0; i <= a; i++) {
cout << A[i].x << " "<< A[i].y<<endl;
}
}

int main() {
init();
solve();
return 0;
}

22
13. Main2
#include<bits/stdc++.h>
using namespace std;

int arr[1000][1000];
int n, m, cd = 0, hd = 0, cc, hc;
int ch[] = {0, -1, 0, 1};
int ck[] = {-1, 0, 1, 0};
int q;

void bfs(int a, int b){


if(arr[a][b] == 1){
q++;
arr[a][b] = 0;
if(n == hc && m == cc){
cout << q;
} else {
for(int i = 0; i < 4;i++)
bfs(n + ch[i], m + ck[i]);
}
}
}

int main(){
freopen("dd.inp", "r", stdin);
cin >> n >> m >> cd >> hd >> cc >> hc;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++)
cin >> arr[i][j];
}

23
bfs(hd, cd);
return 0;
}
14. Sắp xếp nhanh
#include<iostream>
using namespace std;

void quick_sort(int k[], int fi, int la){


bool ok = true;
if(fi<la){
int i = fi;
int j = la + 1;
int pivot = k[fi];
while(ok){
i++;
while((k[i] < pivot) && (i<la)) i++;
j--;
while((k[j] > pivot) && (j > fi)) j--;
if(i<j) swap(k[i], k[j]); else ok = false;
}
swap(k[fi], k[j]);
quick_sort(k, fi, j-1);
quick_sort(k, j + 1, la);
} return;
}

int main()
{
int fi;
int la;

24
int k[] = {10, 80, 30, 90, 40, 50, 70};

cout<<"nhap fi: ";


cin>>fi;
cout<<"nhap la: ";
cin>>la;
cout<<" Day sap xep: ";
quick_sort(k[], fi, la);
return 0;

}
Subset_tt1
#include<iostream>
using namespace std;

int main(){
cout << "Nhap so luong phan tu: ";
int n; cin >> n;
int a[n + 2];
for (int i = 1; i <= n; i++){
cin >> a[i];
}

int sum = 0, temp = 0;


int start, _end;
for(int i = 1;i <= n; i++){
if(a[i] > 0){
for(int j = i; j <= n; j++){
temp = 0;
if (a[j] >= 0){

25
for(int k = i; k <= j; k++)
temp += a[k];
if (temp >= sum){
sum = temp;
start = i;
_end = j;
}
}
}
}
}
cout <<"Tong: "<< sum << endl <<"start: "<<start << endl <<"_end :"<< _end << endl;

return 0;
}

15. Subset_tt2
#include<iostream>
using namespace std;

void solve(int &n, int &start, int &_end, int &sum){


int a[100] = {0}, b[100] = {0};
int temp = 0;
for (int i = 1; i <= n; i++){
cin >> a[i];
temp = 0;
for (int j = 0; j <= i; j++){
temp += a[j];
}
b[i] = temp;
if (temp > sum){

26
sum = temp;
start = i;
}
}

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


for(int j = i; j <= n; j++){
temp = 0;
temp = b[j] - b[i - 1];
if(temp >= sum){
sum = temp;
start = i;
_end = j;
}
}
}
}

int main(){
int sum = 0, start = 0, _end = 0;
cout<<"nhap n :";
int n; cin >> n;
solve(n, start, _end, sum);
cout <<"tong :"<< sum <<endl << "start :"<<start << endl<<"_end :"<< _end << endl;
return 0;
}

16. Subset_tt3
#include<iostream>
#include<math.h>
using namespace std;

27
/*int main()
{
int n;
cout<<" so phan tu cua day :";
cin>>n;
int a[n];

int sum = 0;
int msum = 0;
int start, _end;
bool check = false;
bool check1 = true;

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


{
cin>>a[i];
sum = sum + a[i];

if(sum < 0)
{
sum = 0;
check = true;
}
else
{
if (check1){
start = i;
check1 = false;
}

28
if(check){
start = i;
check = false;
} else
_end = i;
msum = max(sum, msum);
}
}

cout<<"tong :"<< msum << endl << "start :" << start + 1 << endl << "end :" << _end + 1;

return 0;
}
*/

int n,m;
int a[100], b[100], f[100][100];

void NhapmangA(){
for(int i = 1; i <=n ; i++){
cout<<"B["<<i<<"]=";
cin>> a[i];
}
}
void NhapmangB(){
for(int j = 1; j <=m; j++){
cout<<"A["<<j<<"]=";
cin>> b[j];

29
}
}
void Tim(){
for (int i=1; i<=m; i++)
for (int j=1; j<=n; j++) {
if (a[i] == b[j])
{
f[i][j] = f[i-1][j-1] + 1;
}else f[i][j] = max(f[i-1][j], f[i][j-1]);
}
cout<<f[m][n];

int main(){
cout<<"Nhap day n:";
cin>>n;
NhapmangA();
cout<<"Nhap day m:";
cin>>m;
NhapmangB();
cout<<" ket qua:";
Tim();
}

17. Th2.1 (số phần tử của dãy)


#include<iostream>
using namespace std;

//const int n = 100;

30
int main()
{
int n;
cout<<" so phan tu cua day :";
cin>>n;
int a[n];

int sum = 0;
int msum = 0;
int start, _end;
int tmp;
bool check = false;

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


{
cin>>a[i];
sum = sum + a[i];

//start = i;
if(sum < 0)
{
sum = 0;
check = true;
}
else
{
msum = max(sum, msum);
int tmp = start;
start = i;
i = tmp;

31
_end = i;
}
}

cout<<"tong :"<< msum << endl << "start :" << start << endl << "end :" << _end;

return 0;
}

18. Th2.2
#include<iostream>
#include<math.h>
using namespace std;

void bubble_sort(int k, int n)


{
for(int i=1; i<= n-1; i++)
{
for(int j = n; j>i; j--)
{
if(k[j] < k[j-1])
{
int tmp = k[j];
k[j] = k[j-1];
k[j-1] = tmp;
}
}
}

return;
}

32
int main()
{
int n;
cout<<"nhap n:";

return 0;
}
19. Tsp_tt1
#include<iostream>
using namespace std;

int n = 5, cost = 999999, j = 0;


int B[6] = {0,1,2,3,4,5};
int m[120][7];
bool check[6] = {true, true, true, true, true, true};
int matrix[6][6] = {
{0,0,0,0,0,0},
{0,0,9,8,14,14},
{0,9,0,22,10,15},
{0,8,22,0,8,4},
{0,14,10,8,0,19},
{0,14,15,4,19,0}
};

int tong() {
int sum = 0;
for (int i = 1; i <= n-1; i++) {
sum += matrix[B[i]][B[i+1]];
}

33
sum += matrix[B[n]][B[1]];
return sum;
}

void out() {
if (tong() < cost) {
cost = tong();
j = 0;
}
if (tong() == cost) {
m[j][0] = B[1];
m[j][1] = B[2];
m[j][2] = B[3];
m[j][3] = B[4];
m[j][4] = B[5];
m[j][5] = B[1];
m[j][6] = tong();
j++;
}
}

void swap(int m, int i) {


int tmp = B[m];
B[m] = B[i];
B[i] = tmp;
}

void Try(int m) {
if (m == n) {
out();

34
}

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


swap(m, i);
Try(m+1);
swap(m, i);
}
}

int main() {
Try(1);
for (int i = 0; i < j; i++) {
cout<<m[i][0]<<" "<<m[i][1]<<" "<<m[i][2]<<" "<<m[i][3]<<" "<<m[i][4]<<" "<<m[i]
[5]<<endl;
}
cout << "chi phi: " << cost << endl;
return 0;

}
Tsp_tt2 // Giải thuật toán nhánh cận
#include<iostream>
using namespace std;

int matrix[100][100]; // ma tráºn chi phÃ


int n, m;
int check[100];
int result[100];
int bestConfig[100];
int minC = 1000000;
int cost = 0;
int start;

35
void inputMatrix(){
cout<<" nhap so dinh:";
cin >> n;
for(int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
cin >> matrix[i][j];
}
for(int i = 0;i < sizeof(result) / sizeof(*result); i++){
result[i] = 0;
bestConfig[i] = 0;
}
}

void Try(int i){


if(i == n){
if (cost + matrix[result[i - 1]][result[0]] < minC){
minC = cost + matrix[result[i - 1]][result[0]];
for(int k = 0;k < n; k++)
bestConfig[k] = result[k];
}
} else {
for(int j = 1; j < n; j++){
if(check[j] == 0 && cost + matrix[result[i - 1]][j] < minC){
result[i] = j;
check[j] = 1;
cost += matrix[result[i - 1]][j];
Try(i + 1);
check[j] = 0;
cost -= matrix[result[i - 1]][j];

36
}
}
}
}

void output(){
for (int i = 0; i < n; i++){
cout << bestConfig[i] + 1 << "->";
}
cout << bestConfig[0] + 1 << endl;
cout << "Chi phi: " << minC << endl;
}

int main(){
inputMatrix();
for(int i = 0;i < n; i++){
for(int j = 0; j < n; j++)
cout << matrix[i][j] << " ";
cout << endl;
}
start = 1;
start--;
result[0] = start;
check[start] = 1;
Try(1);
output();
return 0;
}

20. Tsp_tt3
#include<iostream>

37
using namespace std;
const int MAX = 100000;
int Matrix[100][100], res[101];
int n =0,index=0,Cost = 0;
bool check[MAX];
int vt =0;
int s, d;
void Try(int e) {
res[s] = e;
check[e] = false;
index = MAX;
for (int i = 1; i <= n; i++) {
if (Matrix[e][i] < index && check[i] && Matrix[e][i] != 0) {
index = Matrix[e][i];
vt = i;
}
}

if (s < n) {
s++;
Cost += index;
Try(vt);
}
else {
res[s+1] = d;
Cost += Matrix[vt][d];
}
}
int tong = MAX;
int tr[100];

38
int main() {
cout<<" nhap so dinh:";
cin>>n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++){
cin>>Matrix[i][j];}
for (int i = 1; i <= n; i++) {
for (int i = 1; i <= n; i++)
check[i] = true;
s = 1;
d = i;
Try(i);
if (Cost < tong) {
tong = Cost;
for (int j = 1; j <=n; j++)
tr[j] = res[j];
}
}
cout << tong<<endl;
for (int j = 1; j <= n; j++)
cout<<tr[j]<<endl;
return 0;
}

21. Vd1: dãy con chung lớn nhất


#include <iostream>
using namespace std;
int A[7] = {2,9,4,6,12,1,7};
int B[5] = {6,1,2,4,6};
int F[50][50];
int timsochung(){

39
for(int i = 0; i <= 5; i++){
for(int j = 0; j <= 7; j++){
F[j][i] = 0;
}
}
for(int i = 1; i <= 5; i++){
for(int j = 1; j <= 7; j++){
if(A[i-1] == B[j-1]){
F[j][i] = max(F[j-1][i],F[j][i-1])+1;
}
else{
F[j][i] = max(F[j-1][i],F[j][i-1]);
}
}
}
for(int i=0; i<5;i++){
for(int j = 0; j<7; j++){
cout<<F[i][j]<<" ";
}
cout<<endl;
}

}
void inkq(){
for(int i=5; i>=0; i--){
for(int j=7; j>=0; j--){
if(i==0 || j==0) break;
if(A[i-1] == B[j-1]){
cout<<B[j-1]<<" ";
i--;

40
}
}
}
}

int main(){
timsochung();
cout<<"Day con chung lon nhat: ";
inkq();
}
22. Tính tổng
#include<iostream>
#include <fstream>
using namespace std;

int const Max = 1000;


int n;
int a[Max];
int L[Max][Max], T;
int B[Max];

void docfile(){
ifstream mofile ("tong.inp");
if (mofile.fail())
cout << "Mo file bi loi!" <<endl;
while (!mofile.eof())
{
int n;
mofile>> n;
cout << n << " ";

41
}

cout<<endl;
mofile.close();
}

void tinhtong(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
{
L[i][0]=1;
if(j>=a[i])
L[i][j]= L[i-1][j]+ L[i-1][j-a[i]];
else
L[i][j]= L[i-1][j];
}
}
}

void xuat(){
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
cout<<"Tong: "<<L[i][j]<<" ";
}
cout<<endl;
}
}

23. Một loại vi khuẩn đặc biệt A được nuôi cấy trong môi trường dinh dưỡng đặc biệt, cứ sau một
giờ sẽ sản sinh thêm
một số cá thể mới. Người ta nhận thấy rằng vào thời điểm giờ chẵn, số vi khuẩn sẽ tăng gấp đôi,

42
nhưng vào thời điểm
giờ lẻ, số vi khuẩn chỉ tăng thêm tối đa 30%. Với N con vi khuẩn ban đầu, sau H giờ đồng hồ, sẽ
có tối đa bao nhiêu vi
khuẩn?
Input
Hai số N và H.
Output
Số lượng vi khuẩn tối đa có thể có.
Giới hạn
Thời gian: 1s
Bộ nhớ: 100 MB
Mã nguồn: 50 KB
Ví dụ
Input Output
11 1
12 2
2 10 170
2 100 131336850

#include<iostream>
using namespace std;

int main(){
long long n, h;
cin >> n >> h;
for(int i=1; i<=h; i++){
if(i%2==0) n = n*2;
else n = n + (int)(n * 0.3);
}
cout << n;
return 0;
}

24. Cho dãy A gồm N số nguyên đôi một khác nhau. Đếm xem có bao nhiêu cách lấy ra từ A đúng 5
phần tử để 5 phần tử
đó tạo thành một cấp số cộng.
Input
Dòng 1: Số N (N < 200)
Dòng 2: N số nguyên của A
Output
Số cách chọn 5 phần tử từ A.
Giới hạn
Thời gian: 1s
Bộ nhớ: 100 MB
Mã nguồn: 50 KB
Ví dụ
Input Output
5 1

43
51324
6
1
10 7 0 -2 1 4
10
8
9753124680

#include<iostream>
#include<algorithm>
using namespace std;

int main(){
int N; cin >> N;
int A[N];
for(int i = 0; i < N; i++) cin >> A[i];
sort(A, A + N);
int demcapsocong = 0;
for(int i = 0; i < N; i++){
for(int j = i + 1; j < N; j++){
int x = A[j] - A[i];
int y = A[j] + x;
int check = true;
for(int k = 2; k < 5; k ++){
if(find(A, A + N, y) - A < N) y += x;
else check = false;
}
if(check) demcapsocong++;
}
}
cout<<demcapsocong;
return 0;
}

25. Cho số nguyên dương N, đếm xem có bao nhiêu dãy số chỉ gồm các số 2, 3 hoặc 4 có tổng đúng bằng
N.
Input
Số N.
Output
Số lượng dãy số.
Giới hạn
Thời gian: 1s
Bộ nhớ: 100 MB
Mã nguồn: 50 KB
Ví dụ
Input Output
1 0
2 1
3 1

44
2
5
(dãy 2+3 và dãy 3+2)
#include<iostream>
using namespace std;

long long kq[90];

long long dem(int N){


if(N < 2)
return 0;
if(N == 2 or N == 3 or N == 4)
return 1;
if(kq[N]!= -1)
return kq[N];
return dem(N-2) + dem(N-3) + dem(N-4);
}

int main(){
int N; cin>>N;
for(int i = 0; i < 90; i++) kq[i]=-1;
for(int i = 1; i <= N; i++){
kq[i] = dem(i);
}
cout<<dem(N);
return 0;
}

26. Dãy con tăng dài nhất

#include <iostream>
#include <algorithm>
using namespace std;

int main ()
{
int n;
cin>>n;
long arr[1003];
long F[1003];
for (int i=1; i<=n; i++)

45
cin>>arr[i];
arr[0] = 0;
F[0] = 0;
for (int i=1; i<=n; i++)
{
F[i] = 1;
for (int j=i-1; j>=1; j--)
{
if (arr[i]>arr[j])
{
F[i]=max(F[i], F[j]+1);
}
}
}

long dmax = 1;
for (int i=1; i<=n; i++)
if (F[i]>=dmax)
dmax = F[i];
cout<<dmax;
return 0;
}

27. Đếm
#include <iostream>
using namespace std;

const int MAX = 100;

int a[MAX], n = 20, dem = 0;

// in phuong an xep hau


void print() {
dem++;
cout << "Phuong an thu " << dem << ":" << endl;

46
for (int i = 1; i <= n; i++, cout << endl)
for (int j = 1; j <= n; j++)
if (j == a[i]) cout << " x";
else cout << " .";
}

bool check(int h, int c) {


// kiem tra cot
for (int i = 1; i < h; i++)
if (c == a[i]) return false;
// kiem tra duong cheo chinh
for (int i = 1; i < h; i++)
if (c-h == a[i]-i) return false;
// kiem tra duong cheo chinh
for (int i = 1; i < h; i++)
if (c+h == a[i]+i) return false;
return true;
}

// dat quan hau o dong thu k


void dat(int k) {
if (k > n) {
print(); return;
}
for (int i = 1; i <= n; i++)
if (check(k, i)) {
a[k] = i; dat(k+1);
}
}

int main() {
dat(1);
}

28. Tổ hợp
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int k, n;
int A[1000];
void result()
{
for (int i = 1; i <= k; i++)

47
{
cout << setw(3) << A[i];
}
cout << endl;
}
void Back(int i)
{
for (int j = A[i - 1] + 1; j <= n - k + i; j++)
{
A[i] = j;8
if (i == k)
{
result();
}
else
{
Back(i + 1);
}
}
}
void tohop()
{
if (k >= 0 && k <= n)
{
A[0] = 0;
Back(1);
}
else
{
cout << "Yeu cau khong duoc hoan thanh\n";

48
}
}
int main()
{
cout<<"Nhap n: ";
cin>>n;
cout<<"Nhap k: ";
cin>>k;
tohop();
return 0;
}

29. Snack
#include<bits/stdc++.h>
using namespace std;

// Returns the maximum value with knapsack of


// W capacity
int unboundedKnapsack(int W, int n,
int val[], int wt[])
{
// dp[i] is going to store maximum value
// with knapsack capacity i.
int dp[W+1];
memset(dp, 0, sizeof dp);

// Fill dp[] using above recursive formula


for (int i=0; i<=W; i++)
for (int j=0; j<n; j++)
if (wt[j] <= i)

49
dp[i] = max(dp[i], dp[i-wt[j]] + val[j]);

return dp[W];
}

// Driver program
int main()
{
int W = 100;
int val[] = {10, 30, 20};
int wt[] = {5, 10, 15};
int n = sizeof(val)/sizeof(val[0]);

cout << unboundedKnapsack(W, n, val, wt);

return 0;
}

30. Seach

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n = 9;
int a[] = { 1, 2, 2, 3, 6, 6, 6, 7, 8};
cout << "find(6): " << find(a+0, a+n, 6) - a << endl;
cout << "binary_search(5): " << binary_search(a+0, a+n, 5) << endl;
cout << "binary_search(6): " << binary_search(a+0, a+n, 6) << endl;
cout << "lower_bound(5): " << lower_bound(a+0, a+n, 5) - a << endl;

50
cout << "lower_bound(6): " << lower_bound(a+0, a+n, 6) - a << endl;
cout << "upper_bound(5): " << upper_bound(a+0, a+n, 5) - a << endl;
cout << "upper_bound(6): " << upper_bound(a+0, a+n, 6) - a << endl;
}

31. Sau con cung dai nhat


#include <iostream>
#include <math.h>
using namespace std;

int n;
string S[1003];
void read()
{
cin>>n;
for (int i=1; i<=n; i++)
cin>>S[i];
}

int F[31][31];
void init()
{
for (int i=0; i<=30; i++)
for (int j=0; j<=30; j++)
F[i][j]=0;
}

int LCS(string S1, string S2)


{
init();

51
for (int i=0; i<S1.length(); i++)
{
for (int j=0; j<S2.length(); j++)
{
if (S1[i]==S2[j])
F[i+1][j+1]=F[i][j]+1;
else
F[i+1][j+1]=max(F[i][j+1], F[i+1][j]);
}
}
return F[S1.length()][S2.length()];
}

int main ()
{
int t;
cin>>t;
while (1)
{
if (t==0) break;
t--;
read();
int maxLCS = 0;
for (int i=1; i<=n; i++)
{
for (int j=i+1; j<=n; j++)
maxLCS = max(maxLCS, LCS(S[i], S[j]));
}
cout<<maxLCS<<endl;
}

52
return 0;
}

32. Lũy thừa


#include<iostream>
using namespace std;

int main()
{
int a;int b;
cout<<"\n Nhap a= "; cin>>a; cout<<endl;
cout<<"\n Nhap b= "; cin>>b; cout<<endl;

long Luythua =a ;
int temp=0;

for(int i=1; i<b; i++)


{
Luythua *= a;
temp=Luythua % 1000000000;
}
cout<<temp<<endl;
system("pause");
return 0;
}

33. Moo
#include <iostream>
using namespace std;

53
struct data
{
long long leng;
long long x;
long long y;
long long z;
};
struct data S[200];
void khoitao ()
{
S[0].leng = 3;
S[0].x = 0;
S[0].y = 3;
S[0].z = 3;
}

char Dequy (long long n, int a)


{
if (1<=n && n<=S[a].x) return Dequy (n, a-1);
else if (S[a].x<n && n<=S[a].y)
{
n=n-S[a].x;
if (n==1) return 'm';
else return 'o';
}
else if (S[a].y<n && n<=S[a].z)
{
n=n-S[a].y;
return Dequy (n, a-1);

54
}
}

int main ()
{
khoitao();
int i=0;
long long N;
cin>>N;
while (1)
{
if (S[i].leng>=N) break;
i++;
S[i].x=0+S[i-1].leng;
S[i].y=S[i].x+1+i+2;
S[i].z=S[i].y+S[i-1].leng;
S[i].leng=S[i].z;
}
cout<<Dequy (N, i);

return 0;
}

34. Fibo
#include <iostream>
using namespace std;
int Fibonacci(int n)
{
if (n == 1 || n == 2)
return 1;

55
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
int main()
{
int n;
cout << "nhap n: ";
cin >> n;
cout << "So Fibonacci thu " << n << " la: " << Fibonacci(n);
return 0;}

#include <iostream>
using namespace std;
int H;
long long N;
long long dequy(int i)

56
{
if(i>H) return N;
if (i % 2 == 0)
N = N*2;
else
N = N*1.3;
return dequy(i+1);
}
int main()
{
cin >> N >> H;
// for (int i = 1; i <= H; i++)
// {
// if (i % 2 == 0)
// N = (2 * N);
// else
// N = N*1.3;
//}
cout<<dequy(1);
return 0;
// cout<<N;
}

57
#include <iostream>
using namespace std;
const int Max = 100;
int n, a[Max];
int KiemTraCapSoCong(int a[], int n)
{
int flag = 0;
for (int i = 0; i < n; i++)
{
int d = a[1] - a[0];
for (int i = 2; i < n - 1; i++)
{
if ((a[i + 1] - a[i]) != d)
{

58
flag = flag + 1;
break;
}
}
}
return flag;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
int flag = KiemTraCapSoCong(a, n);
if (flag == 5)
{
cout << flag/5;
}
else
return 0;
}

59
#include <iostream>
using namespace std;
int n;int count = 0;
int gen(int n, int X[], int T[], int i)
{
for (int j = X[i - 1]; j <= (n - T[i - 1]); j++)
{
if ((X[i] = j) && ((X[i] == 2) || (X[i] == 3)))
T[i] = T[i - 1] + j;

if (T[i] == n)
{
count = count + 1;
}
else
{
gen(n, X, T, i + 1);

60
}
}
return count;
}
int main()
{
cin >> n;
int X[n + 1];
int T[n + 1];
T[0] = 0;
X[0] = 1;
cout << gen(n, X, T, 1);
}

61
BCATM3 - ATM 3
Máy ATM tại cổng trường Học viện Công nghệ Bưu chính viễn thông hiện chứa tiền có 9 mệnh
giá: 500, 200, 100, 50, 20, 10, 5, 2, 1 (đơn vị Nghìn đồng). Mỗi mệnh giá có vô số tờ tiền.
Khi bạn rút một lượng tiền X (đơn vị Nghìn đồng), máy ATM sẽ tính toán để đưa ra các tờ tiền
sao cho tổng tiền là X và số tờ tiền là ít nhất có thể. Bạn hãy viết chương trình giúp ATM giải bài
toán này nhé.

Input
Dòng đầu tiên nhập N là số bộ test (0 < N <= 50 000)
N dòng sau, mỗi dòng tương ứng với 1 bộ test, bao gồm số tiền X (0 < X <= 10 000) – là lượng
tiền bạn cần rút.

Output
In kết quả trên N dòng, dòng thứ i là tổng số tờ tiền mà máy ATM sẽ đưa ra tương ứng với test
thứ i.

Example
Input:
2
560
732

Output:
3
5

Giải :
#include <iostream>
using namespace std;
const int MAX = 9;

62
int c[MAX] = {500, 200, 100, 50, 20, 10, 5, 2, 1};
int n;
void changeMoney(int c[], int a[], int n)
{
for (int i = 0; i < n; i++){
int count = 0;
int tien = a[i];
for (int j = 0; j < MAX; j++){
if ((tien / c[j]) >= 1){
int tam = tien / c[j];
tien = tien - c[j] * tam;
count = count + tam;
}
if (tien == 0){
break;
}
}
cout << count << endl;
}
}
int main(){
cin >> n;
int a[n];
for (int i = 0; i < n; i++){
cin >> a[i];
}
changeMoney(c, a, n);
return 0;
}

63
PTIT135G - Blackjack
Cho một tập N quân bài, mỗi quân chứa một số nguyên dương. Bạn cần phải chọn ra ba quân bài
sao cho tổng các số trên 3 quân bài gần với số M nhất và không vượt quá M.

Input
Dòng 1 chứa 2 số N và M. (N <= 100, M <= 500 000)
Dòng 2 chứa N số nguyên dương, mỗi số không quá 100 000.

Output
In ra tổng 3 quân gần M nhất và không vượt quá M.
Input luôn đảm bảo tồn tại đáp số.

Example
Input:
5 21
56789

Output:
21

Giải
#include <iostream>
#include<string.h>
#include <math.h>
using namespace std;
int main()
{
int n, m;
int a[1000];
cin >> n >> m;
for (int i = 0; i < n; i++)

64
cin >> a[i];
int max = 0;
int sum = 0;
for (int i = 0; i < n-2; i++)
{
for (int j = i + 1; j < n - 1; j++)
{
for (int h = j + 1; h < n; h++)
{
sum = a[i] + a[j] + a[h];
if (sum > max&& sum <= m)
max = sum;
}
}
}
cout << max << endl;
return 0;
}

TGSO - Tam giác số


Một nửa ma trận vuông cấp n là một tam giác vuông cân, trên mỗi ô của tam giác có
chứa một số nguyên.
Tại một ô của tam giác là (i,j), có thể đi đến 1 trong 3 ô là (i+1,j-1), (i+1,j) và
(i+1,j+1). Có thể xem ở hình dưới:
  (i,j)  
i+1,j-1 i+1, j i+1,j-1
Hãy tìm một hành trình từ đỉnh tam giác xuống đáy của tam giác sao cho tổng các ô
trên hàng trình đi là lớn nhất.

65
Input:
- Dòng 1 chứa N (1 <= N <= 100)
- N dòng tiếp theo, dòng thứ i chứa i số nguyên
Output:
- Dòng 1 là số S: tổng giá trị lớn nhất trên hành trình
- N dòng tiếp theo, dòng thứ i chứa giá trị j tức là sẽ đi qua dòng i cột j trên hành trình.
Ví dụ
INPUT OUTPUT Giải thích ví dụ
5 32 Các số được bôi đậm
là các số đi trên hành
3 1
trình
1 5 2
7 2 8 1
8 3 5 6 1
1 9 3 7 3 2

Giải
#include <iostream>

#include <bits/stdc++.h>

using namespace std;

const int MAX = 101;

int a[MAX][MAX], f[MAX], n, way[MAX];

int main(){

cin >> n;

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

way[i] = -1;

for (int j = 0; j < MAX; j++) {

66
a[i][j] = 0; } }

for(int i = 1; i < n+1; i++)

for(int j = 1; j < i+1; j++)

cin >> a[i][j];

int x = n, y = 0, first_pos; int m = a[x][1];

for(int i = 2; i < n+1; i++){

if(m < a[x][i]) {

m = a[x][i]; y = i; } }

first_pos = y;

int t = a[x][y];

for(int i = n; i >=1 ; i--){

for(int j = n; j >= 1; j--) {

way[j] = y; if(x == 1) break;

int w1, w2, w3, w;

w1 = a[x-1][y-1];

w2 = a[x-1][y];

w3 = a[x-1][y+1];

w = max(w1,

max(w2, w3));

t += w; x--;

if(w == w1) y--;

if(w == w3) y++; } } cout << t << endl;

way[n] = first_pos;

for(int i = 1; i < n+1; i++)

{ cout << way[i] << endl;}

return 0; }

67
Tổ hợp

Cho hai số nguyên dương n và k (1 <= k <= n <= 20). Sinh mọi tổ hợp chập k của n phần tử.
Ví dụ:
Input:
52
Output
12
13
14
15
23
24
25
34
35
4 5 

Giải
#include <iostream>
using namespace std;
const int MAX = 100;
int a[MAX], n, k;
void print(int n) {
for (int i = 1; i <= k; i++) cout <<a[i]<<" " ; cout << endl;
}
void gen(int id) {
if (id > k) {
print(n);
return;
}

68
for (int i = a[id-1] + 1; i <= n-k+id; i++){
a[id] = i; gen(id+1);
}
}
int main(){
do{
cin>>n;
}while(n>20 || n < 1);
do{
cin>>k;
}while( k > n || k < 1);
gen(1);
return 0;
}

HOANVI - Hoán vị
Cho số nguyên N. Hãy sinh mọi hoán vị từ 1 đến N

Input

 Một số nguyên dương N (N<=10)

Output

 Tập hoán vị được sắp xếp theo thứ tự từ điển

Example
Input:
3

Output:

69
123
132
213
231
312
3 2 1 

Giải
#include <iostream>
using namespace std;
int n, kq[0], dd[10];
void xuat(){
for (int j=1; j<=n; j++)
cout<< kq[j]<<" ";
cout << endl;
}
void kiem(int i){
if (i>n) xuat();
for (int j=1; j<=n; j++)
if (dd[j]==0){
dd[j]=1;
kq[i]=j;
kiem(i+1);
dd[j]=0;
}
}
int main(){

70
cin >> n;
for (int i=1; i<=9; i++)
dd[i]=0;
kiem(1);
}

HVTTONG - Phân tích


Liệt kê tất cả các cách phân tích số nguyên dương N thành tổng các số nguyên dương
nhỏ hơn. Biết rằng hai cách phân tích 4 = 1 + 1 + 2 và 4 = 1 + 2 + 1 được coi là 1.

Input

 Số nguyên dương N (N<=30)

Output

 Đưa ra biểu thức phân tích N thành tổng các số nhỏ hơn

Example
Input:
4

Output:
4=1+1+1+1
4=1+1+2
4=1+3
4=2+2
4=4

71
Giải
#include <iostream>
using namespace std;
int x[30],t[30],n;
void xuatmang(int k)
{
cout<<"\n"<<n<<"=";
for (int i=1;i<k;i++)
cout<<x[i]<<"+";
cout<<x[k];
}
void Phantich(int i)
{
for(int j=x[i-1];j<=((n-t[i-1])/2);j++)
{
x[i]=j;
t[i]=t[i-1]+j;
Phantich(i+1);
}
x[i]=n-t[i-1];
xuatmang(i);
}
int main()
{
cin>>n;
x[0]=1;
t[0]=0;
Phantich(1);

72
}

Đếm số cách sinh tổng S


Cho dãy số nguyên gồm N phần tử A1, A2, ..., AN, và số nguyên S.
Có bao nhiêu cách tạo được tổng S bằng cách chọn ra một vài phần tử trong dãy A.
INPUT:
- Dòng 1 chứa hai số N và S
- Dòng 2 chứa N số A1, A2, ..., AN
OUTPUT:
- Số cách tạo tổng S. Vì số cách có thể rất lớn nên lấy kết quả là phần dư của số cách
đó với 10 +79

Ví dụ
INPUT OUTPUT Giải thích
57 3 Cách 1: chọn số 7
17633 Cách 2: chọn 1 và 6
Cách 3: chọn 1, 3 và 3

Giải
#include <iostream>
using namespace std;
const int MAX = 10000;
const int SHL = 1e9+7;
int n,s,a[MAX],b[MAX][MAX];

int F(int s, int n){


if(n==0) return (s==0)?1:0;
if(b[s][n]==-1){

73
if(s>=a[n])b[s][n]=(F(s,n-1)+F(s-a[n],n-1))%SHL;
else b[s][n]=F(s,n-1)%SHL;
}
return b[s][n];
}
int main(){
cin>>n;
cin>>s;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=0;i<MAX;i++)
for(int j=0;j<MAX;j++) b[i][j]=-1;
cout<<F(s,n);
}
1. Travelling Salesman Problem

Cho n thành phố đánh số từ 1 đến n và các tuyến đường giao thông hai chiều giữa chúng, mạng lưới
giao thông này được cho bởi mảng C[1…n, 1…n] ở đây C[i][j] = C[j][i] là chi phí đi đoạn đường trực tiếp
từ thành phố I đến thành phố j. Một người du lịch xuất phát từ thành phố 1, muốn đi thăm tất cả các
thành phố còn lại mỗi thành phố đúng 1 lần và cuối cùng quay lại thành phố 1. Hãy chỉ ra chi phí ít nhất
mà người đó phải bỏ ra.

#include <iostream> #include <algorithm> using namespace std;

const int MAX = 15; int C[MAX][MAX], check[MAX],n, MaxC =1e9;

void test(int tong, int bd, int count)

{ if(count <n-1 && tong<MaxC){

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

if(check[i]==0){

check[i]=1;

test(tong+C[bd][i],i,count+1);

check[i]=0; }}}

74
else MaxC=min(MaxC,tong+C[bd][0]); }

int main() { cin>>n;

for(int i=0;i<n;i++) check[i] =0;// khoi tao 1 mang danh dau cac dinh =0 chua di den

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

for(int j=0;j<n;j++)

cin>>C[i][j];

test(0,0,0);

cout<<MaxC; }

Bai 5: Đếm xâu con: Cho một xâu s (độ dài không quá 200 ký tự) chỉ gồm các ký tự từ 'A' tới 'Z'. Đếm số
lượng xâu con liên tiếp khác nhau nhận được từ s.

#include <iostream> #include <algorithm> #include <string> using namespace std;

const int MAX = 200; string a , s[MAX];

int xau(string a)

{ int count =0;

for(int i=0; i< a.length(); i++)

{ int d = 1;

for(int j = i; j< a.length(); j++) { s[d] = a.substr(j-i,i+1); d++; }

sort(s+0, s+d);

for(int t =0; t< d-1; t++) if(s[t] != s[t+1]) count ++; }

return count; }

int main() { cin>>a; cout<<xau(a); }

Bai 9: Bí kíp luyện rồng

#include <iostream> using namespace std;

int main(){ long long hitcup, n, count = 0; cin >> hitcup >> n;

long long chiso[n], tang[n]; bool killed[n];

for(int i=0; i<n; i++) { cin >> chiso[i] >> tang[i]; killed[i] = false; }

for(int i=0; i<n; i++){ for(int j=0; j<n; j++) { if(!killed[j] and hitcup > chiso[j]){ count += 1; hitcup += tang[j];
killed[j] = true; } } }

if(count == n) cout << "YES"; else cout << "NO" << endl << n-count; return 0; }

75
Bai 10: Chia nhóm : Cho dãy số A gồm N số (N <= 10) a1, a2, .., an. và một số nguyên dương K(1 < K < N).
Hãy đưa ra số cách để chặt dãy số thành K nhóm (các phần tử trong nhóm là liên tiếp) mà các nhóm có
tổng bằng nhau.

#include <iostream> using namespace std;

const int Max = 100; int n, k; int A[Max]; int count = 0;

void nhap() { cin >> n >> k; A[0] = 0;

for (int i = 1; i <= n; i++) { int tmp; cin >> tmp; A[i] = A[i - 1] + tmp; } }

int socach(int sum, int l, int r, int K) {

if (l > r) return 0; if (K == 1) { if (sum == A[r] - A[r - 1]) { count++; return 1; }

else return 0; } else if (K == 0) return 0;

else { int L = l; for (int i = l; i <= r; i++) { if (A[i] - A[L - 1] == sum) { socach(sum, i + 1, r, K - 1); } } } return
count; }

int main() { nhap(); if (k == 1) { count = 1; } else { for (int i = 1; i <= n; i++)

{ int label = A[i]; socach(label, i + 1, n, k - 1); } } cout << count; return 0; }

Bai 11 : LÁT GẠCH 2 Ở công viên thành phố có 1 bức tường, trên bức tường người ta gắn 1 bức phù
điêu tái hiện sự kiện lịch sử của thành phố. Ở dưới chân bức tường người ta dự định ốp gạch trang trí
trên 1 khoảng hình chữ nhật có kích thước 2xN có 2 loại gạch 1x2 và 2x2. Hãy xác định số cách ốp.

#include <iostream> using namespace std;

const int MAX = 30; int a[MAX], f[MAX], T;

int main()

{ f[1] =1; f[2]= 3;

for(int i=3; i< MAX; i++) f[i] = f[i-1] + 2*f[i-2];

cin>>T;

for(int i=0;i<T;i++) cin>>a[i];

for(int i=0;i<T;i++) cout<<f[a[i]]<<endl; }

Bai 12 : Tháp Hà Nội Có 3 cọc gỗ và N miếng gỗ tròn có bán kính từ nhỏ đến lớn. Ban đầu tất cả N
miếng gỗ đặt chồng lên nhau ở cọc số 1 theo thứ tự nhỏ ở trên lớn ở dưới. Hãy chuyển N miếng gỗ này
sang cọc 3

#include <iostream> using namespace std;


int trans(int n, int a, int b,int c)
{ if(n==1) { cout<<"Di chuyen tu "<<a<<" sang "<<c<<endl; return 0; }

76
trans(n-1,a,c,b); trans(1,a,b,c); trans(n-1,b,a,c);}
int main() { trans(3,1,2,3); }

77

You might also like