This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/NMLT-NTTMK-K18/6-290-recursion
- Loading branch information
Showing
8 changed files
with
402 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,34 @@ | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <cmath> | ||
using namespace std; | ||
|
||
float Tinh(float, int); | ||
|
||
int main() | ||
{ | ||
int n; | ||
float x; | ||
cout << "Nhap x: "; | ||
cin >> x; | ||
cout << "Nhap n: "; | ||
cin >> n; | ||
|
||
cout << "\nKet qua cua S(" << n << ") la: " << setw(5) << Tinh(x, n) << endl; | ||
return 0; | ||
} | ||
|
||
float Tinh(float x, int n) | ||
{ | ||
if (n == 0) | ||
{ | ||
return 0; | ||
} | ||
if (n == 1) | ||
{ | ||
return x; | ||
} | ||
float a = Tinh(x, n - 1); | ||
float b = Tinh(x, n - 2); | ||
return ((1 + x/n) * a - x/n * b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,86 @@ | ||
#include <iostream> | ||
using namespace std; | ||
#include<stdlib.h> | ||
#include<stdio.h> | ||
|
||
void merge(int arr[], int l, int m, int r) | ||
{ | ||
int i, j, k; | ||
int n1 = m - l + 1; | ||
int n2 = r - m; | ||
|
||
int L[1000], R[1000]; | ||
|
||
for (i = 0; i < n1; i++) | ||
L[i] = arr[l + i]; | ||
for (j = 0; j < n2; j++) | ||
R[j] = arr[m + 1 + j]; | ||
|
||
i = 0; | ||
j = 0; | ||
k = l; | ||
while (i < n1 && j < n2) | ||
{ | ||
if (L[i] <= R[j]) | ||
{ | ||
arr[k] = L[i]; | ||
i++; | ||
} | ||
else | ||
{ | ||
arr[k] = R[j]; | ||
j++; | ||
} | ||
k++; | ||
} | ||
|
||
while (i < n1) | ||
{ | ||
arr[k] = L[i]; | ||
i++; | ||
k++; | ||
} | ||
|
||
while (j < n2) | ||
{ | ||
arr[k] = R[j]; | ||
j++; | ||
k++; | ||
} | ||
} | ||
|
||
void mergeSort(int arr[], int l, int r) | ||
{ | ||
if (l < r) | ||
{ | ||
int m = l + (r - l) / 2; | ||
|
||
mergeSort(arr, l, m); | ||
mergeSort(arr, m + 1, r); | ||
|
||
merge(arr, l, m, r); | ||
} | ||
} | ||
|
||
|
||
void printArray(int A[], int size) | ||
{ | ||
int i; | ||
for (i = 0; i < size; i++) | ||
printf("%d ", A[i]); | ||
printf("\n"); | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
int arr[] = { 12, 11, 13, 5, 6, 7 }; | ||
int arr_size = sizeof(arr) / sizeof(arr[0]); | ||
|
||
printf("Given array is \n"); | ||
printArray(arr, arr_size); | ||
|
||
mergeSort(arr, 0, arr_size - 1); | ||
|
||
return 0; | ||
printf("\nSorted array is \n"); | ||
printArray(arr, arr_size); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,42 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
void generate_permutations(vector<int>& seq, vector<int>& curr_perm, vector<bool>& used) { | ||
if (curr_perm.size() == seq.size()) { | ||
for (int i = 0; i < curr_perm.size(); i++) { | ||
cout << curr_perm[i] << " "; | ||
} | ||
cout << endl; | ||
return; | ||
} | ||
|
||
for (int i = 0; i < seq.size(); i++) { | ||
if (!used[i]) { | ||
used[i] = true; | ||
curr_perm.push_back(seq[i]); | ||
generate_permutations(seq, curr_perm, used); | ||
used[i] = false; | ||
curr_perm.pop_back(); | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
vector<int> seq; | ||
int n; | ||
cout << "Enter number of elements: "; | ||
cin >> n; | ||
cout << "Enter elements: "; | ||
for (int i = 0; i < n; i++) { | ||
int x; | ||
cin >> x; | ||
seq.push_back(x); | ||
} | ||
vector<int> curr_perm; | ||
vector<bool> used(n, false); | ||
generate_permutations(seq, curr_perm, used); | ||
|
||
return 0; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,41 @@ | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
void Nhap(float[], int&); | ||
void LietKe(float[], int); | ||
|
||
int main() | ||
{ | ||
|
||
float a[100]; | ||
int n; | ||
Nhap(a, n); | ||
LietKe(a, n); | ||
return 0; | ||
} | ||
|
||
void Nhap(float a[], int& n) | ||
{ | ||
cout << "Nhap n: "; | ||
cin >> n; | ||
cout << "Nhap mang: "; | ||
srand(time(NULL)); | ||
for (int i = 0; i < n; i++) | ||
a[i] = rand() % 201 - 100.00; | ||
cout << "\nMang ban dau la: "; | ||
for (int i = 0; i < n; i++) | ||
cout << setw(5) << a[i]; | ||
cout << endl; | ||
cout << "Cac so duong trong mang la: "; | ||
} | ||
|
||
void LietKe(float a[], int n) | ||
{ | ||
if (n == 0) | ||
return; | ||
LietKe(a, n - 1); | ||
if (a[n - 1] > 0) | ||
cout << setw(10) << setprecision(3) << a[n - 1]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,41 @@ | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
void Nhap(float[], int&); | ||
void LietKe(float[], int); | ||
|
||
int main() | ||
{ | ||
|
||
float a[100]; | ||
int n; | ||
Nhap(a, n); | ||
LietKe(a, n); | ||
return 0; | ||
} | ||
|
||
void Nhap(float a[], int& n) | ||
{ | ||
cout << "Nhap n: "; | ||
cin >> n; | ||
cout << "Nhap mang: "; | ||
srand(time(NULL)); | ||
for (int i = 0; i < n; i++) | ||
a[i] = rand() % 201 - 100.00; | ||
cout << "\nMang ban dau la: "; | ||
for (int i = 0; i < n; i++) | ||
cout << setw(5) << a[i]; | ||
cout << endl; | ||
cout << "Cac vi tri am trong mang la: "; | ||
} | ||
|
||
void LietKe(float a[], int n) | ||
{ | ||
if (n == 0) | ||
return; | ||
LietKe(a, n - 1); | ||
if (a[n - 1] < 0) | ||
cout << setw(10) << n-1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,60 @@ | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
void Nhap(float[], int&); | ||
void LietKe(float[], int); | ||
float LonNhat(float[], int); | ||
|
||
int main() | ||
{ | ||
|
||
float a[100]; | ||
int n; | ||
Nhap(a, n); | ||
LietKe(a, n); | ||
return 0; | ||
} | ||
|
||
void Nhap(float a[], int& n) | ||
{ | ||
cout << "Nhap n: "; | ||
cin >> n; | ||
cout << "Nhap mang: "; | ||
srand(time(NULL)); | ||
for (int i = 0; i < n; i++) | ||
cin >> a[i]; | ||
cout << "\nMang ban dau la: "; | ||
for (int i = 0; i < n; i++) | ||
cout << setw(5) << a[i]; | ||
cout << endl; | ||
cout << "Cac vi tri tai do la gia tri lon nhat la: "; | ||
} | ||
|
||
void LietKe(float a[], int n) | ||
{ | ||
if (n == 0) | ||
return; | ||
float lc = LonNhat(a, n - 1); | ||
if (lc < a[n - 1]) | ||
{ | ||
cout << setw(6) << n - 1; | ||
return; | ||
} | ||
if (lc == a[n - 1]) | ||
cout << setw(6) << n - 1; | ||
LietKe(a, n - 1); | ||
|
||
} | ||
|
||
float LonNhat(float a[], int n) | ||
{ | ||
if (n == 1) | ||
return a[0]; | ||
float lc=LonNhat(a, n - 1); | ||
if (a[n - 1] >= lc) | ||
lc = a[n - 1]; | ||
return lc; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.