1. Tìm kiếm và sắp xếp: // in phuong an xep hau
1. Tìm kiếm và sắp xếp: // in phuong an xep hau
1
bool check(int h, int c) {
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
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>
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]];
}
8
int main(){
n = 5, T = 11;
Try(0, 0);
cout<<maxP<<endl;
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;
}
}
10
}
7. Bubble_ct2
#include<iostream>
using namespace std;
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;
}
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;
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;
}
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};
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;
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;
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;
}
}
}
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;
}
}
}
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;
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;
int main()
{
int fi;
int la;
24
int k[] = {10, 80, 30, 90, 40, 50, 70};
}
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];
}
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;
26
sum = temp;
start = i;
}
}
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;
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();
}
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;
//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;
return;
}
32
int main()
{
int n;
cout<<"nhap n:";
return 0;
}
19. Tsp_tt1
#include<iostream>
using namespace std;
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 Try(int m) {
if (m == n) {
out();
34
}
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;
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;
}
}
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;
}
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;
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;
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;
}
#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;
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 << " .";
}
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;
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]);
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;
}
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;
}
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;
}
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;
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;
}
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;
}
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>
int main(){
cin >> n;
way[i] = -1;
66
a[i][j] = 0; } }
m = a[x][i]; y = i; } }
first_pos = y;
int t = a[x][y];
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--;
way[n] = first_pos;
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
Output
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);
}
Input
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
}
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];
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.
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]); }
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.
int xau(string a)
{ int d = 1;
sort(s+0, s+d);
return count; }
int main(){ long long hitcup, n, count = 0; cin >> hitcup >> 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.
for (int i = 1; i <= n; i++) { int tmp; cin >> tmp; A[i] = A[i - 1] + tmp; } }
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; }
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.
int main()
cin>>T;
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
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