De On Tap C TH HDHDD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

///////////tim kiểm nhi phân////

#include<stdio.h>
#include<conio.h>
#include<math.h>
int binarysearch(int a[], int n, int x)
{
int left = 0;
int right = n-1;
int mid;
while (left <= right)
{
mid = (left + right) / 2;
if (x == a[mid]) return mid;
else
if (x < a[mid]) right = mid - 1;
else left = mid + 1;

//else if (x > a[mid]) left = mid + 1;


//else if (x < a[mid]) right = mid - 1;

}
return -1;
}
void nhapmang(int a[], int n)
{
int i ;
for (i = 0; i < n; i++)
{
printf("a[%d]=",i);
scanf_s("%d", &a[i]);
}
}
void xuatmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d]=%d ", i,a[i]);
}
}
void main()
{
int n;
int a[100];
printf("nhap so phan tu n=");
scanf_s("%d", &n);
nhapmang(a, n);
xuatmang(a, n);
int pos=0;
int x;
printf("nhap gia tri can tim kiem x=");
scanf_s("%d", &x);
pos = binarysearch(a, n, x);
printf("vi tri so can tim la: %d", pos);
_getch();
}

/////// tim kiểm tuyen tinh////////

#include<stdio.h>
#include<conio.h>
#include<math.h>
int linearsearch(int x, int N, int a[])
{
int i = 0;
a[N] = x;
while (a[i] != x)
i++;
if (i == N) return -1;
else return i;
}
void nhapmang(int a[], int N)
{
int i;
for (i = 0; i < N; i++)
{
printf("nhap a[%d]:",i);
scanf_s("%d",&a[i]);
}

}
void xuatmang(int a[], int N)
{
int i;
for (i = 0; i < N; i++)
{
printf("a[%d]=%d ", i, a[i]);

}
}
void main()
{
int n;
int a[100];
int x;
printf("nhap so phan tu n: ");
scanf_s("%d", &n);
nhapmang(a, n);
xuatmang(a, n);
printf("\n nhap gia tri can tim kiem x= ");
scanf_s("%d", &x);
int pos = linearsearch(x, n, a);
printf(" vi tri so can tim la: %d", pos);
_getch();
}

//////// sap xep chon trực tiếp//////////////


#include<stdio.h>
#include<conio.h>
#include<math.h>
void hoanvi(int &a, int &b)
{
int temp;

temp = a;
a = b;
b = temp;

}
void selectionsort(int a[], int n)
{
int min ;
for (int i = 0; i < n - 1; i++)
{
min = i;
for (int j = i+1; j < n; j++)
if (a[j] < a[min])
min = j;
hoanvi(a[min], a[i]);
}
}
void main()
{
int n;
int a[100];
printf("nhap so phan tu n: ");
scanf_s("%d", &n);
nhapmang(a, n);
selectionsort(a, n);
printf("mang sau khi sap xep la :");
xuatmang(a, n);
_getch();
}

////////// chèn trực tiếp///////////


#include<stdio.h>
#include<conio.h>
#include<math.h>
void insertionsort(int a[], int n)
{
int pos, i;
int x;
for (i = 1; i < n; i++)
{
x = a[i];
pos = i - 1;
while ((pos >= 0) && (a[pos] > x))
{
a[pos + 1] = a[pos];
pos--;

}
a[pos + 1] = x;
}
}
void nhapmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d] = ", i);
scanf_s("%d", &a[i]);
}
}
void xuatmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d] = %d ", i, a[i]);
}
}
void main()
{
int n;
int a[100];
printf("nhap so phan tu n :");
scanf_s("%d", &n);
nhapmang(a, n);
insertionsort(a, n);
printf(" mang sau khi sap xep la: ");
xuatmang(a, n);
_getch();
}

/// phương pháp đổi chỗ trực tiếp//////////


#include<stdio.h>
#include<conio.h>
#include<math.h>
void swap(int &a, int &b)
{
int temp;
temp = a;
a=b;
b = temp;
}
void inserchangesort(int a[], int n)
{
for (int i = 0; i < n-1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[i])
swap(a[i], a[j]);

}
}
}

void main()
{
int n;
int a[100];
printf("nhap so phan tu cua n :");
scanf_s("%d", &n);
nhapmang(a, n);
inserchangesort(a, n);
printf("mang sau khi sap xep la: ");
xuatmang(a, n);
_getch();

}
//// phương pháp nổi bọt///////////
#include<stdio.h>
#include<conio.h>
#include<math.h>
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void bubblesort(int a[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = n-1; j > i; j--)
{
if (a[j] < a[j - 1])
swap(a[j], a[j - 1]);
}
}
}

void main()
{
int n;
int a[100];
printf("nhap so pha tu cua mang n=");
scanf_s("%d", &n);
nhapmang(a, n);
bubblesort(a, n);
printf(" mang sau khi sap xep la : ");
xuatmang(a, n);
_getch();
}
shellsort_ sắp xếp với độ dài bước giảm dần ( code
trong sách)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void nhapmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d]= ", i);
scanf_s("%d", &a[i]);
}
}
void xuatmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d]= %d ", i, a[i]);
}
}
void shellsort(int a[], int n, int h[], int k)

{
int step, i, j;
int x, len;
for (step = 0; step < k; step++)
{
len = h[step];
for (i = len; i < n; i++)
{
x = a[i];
j = i - len;
while ((x < a[j]) && (j >= 0))
{
a[j + len] = a[j];
j = j - len;
}
a[j + len] = x;
}
}
}
void main()
{
int n;
int a[100];
printf("nhap so phan tu cua mang n:");
scanf_s("%d", &n);
nhapmang(a, n);
shellsort(a, n);
printf("mang sau khi sap xep la : ");
xuatmang(a, n);
}

sắp xêp với độ dài bước giảm dần shellsort (code hạnh)
/*
* C Program to sort an array using Shell Sort technique
*/
#include <stdio.h>
#include<conio.h>
#include<math.h>
void shellsort(int arr[], int num)
{
int i, j, k, tmp;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j < num; j++)
{
for (k = j - i; k >= 0; k = k - i)
{
if (arr[k + i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k + i];
arr[k + i] = tmp;
}
}
}
}
}
int main()
{
int arr[30];
int k, num;
printf("Nhap n: ");
scanf_s("%d", &num);
printf("Nhap mang: \n");

for (k = 0; k < num; k++)


{
scanf_s("%d", &arr[k]);
}
shellsort(arr, num);
printf("Mang sau khi duoc sap xep: ");
for (k = 0; k < num; k++)
printf("%d ", arr[k]);
_getch();
return 0;

//// sắp xếp cây _ headsort/////////


#include<stdio.h>
#include<conio.h>
#include<math.h>
void heapsort(int[], int);
void heapify(int[], int);
void adjust(int[], int);
int main() {
int n, i, a[50];
printf("Nhap n: ");
scanf_s("%d", &n);
printf("Nhap mang: \n");
for (i = 0; i<n; i++)
scanf_s("%d", &a[i]);
heapsort(a, n);
printf("Mang sau khi duoc sap xep: ");
for (i = 0; i<n; i++)
printf("%d ", a[i]);
_getch();
printf("\n");
return 0;

}
void heapsort(int a[], int n) {
int i, t;
heapify(a, n);
for (i = n - 1; i>0; i--) {
t = a[0];
a[0] = a[i];
a[i] = t;
adjust(a, i);
}
}
void heapify(int a[], int n) {
int k, i, j, item;
for (k = 1; k<n; k++) {
item = a[k];
i = k;
j = (i - 1) / 2;
while ((i>0) && (item>a[j])) {
a[i] = a[j];
i = j;
j = (i - 1) / 2;
}
a[i] = item;
}
}
void adjust(int a[], int n) {
int i, j, item;
j = 0;
item = a[j];
i = 2 * j + 1;
while (i <= n - 1) {
if (i + 1 <= n - 1)
if (a[i] <a[i + 1])
i++;
if (item<a[i]) {
a[j] = a[i];
j = i;
i = 2 * j + 1;
}
else
break;
}
a[j] = item;
}

////// sắp xếp dựa trên phân hoạch _quicksort(code sách)/////


#include<stdio.h>
#include<conio.h>
#include<math.h>
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void quicksort(int a[], int l, int r)
{
int i, j;
int x;
x = a[(l + r) / 2];
i = l;
j = r;
do
{
while (a[i] < x) i++;
while (a[j] > x) j--;
if (i <= j)
{
swap(a[i], a[j]);
i++; j--;
}
} while (i < j);
if (l < j)
quicksort(a, l, j);
if (i < r)
quicksort(a, i, r);
}
void nhapmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d]= ", i);
scanf_s("%d", &a[i]);
}
}
void xuatmang(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("a[%d]= %d ", i, a[i]);
}
}
void main()
{
int n;
int a[100];
int l = 0;
printf("nhap so phan tu cua mang n= ");
scanf_s("%d", &n);
int r = n - 1;
nhapmang(a, n);
quicksort(a, l, r);
printf("mang sau khi sap xep la : ");
xuatmang(a, n);
_getch();
}

/// sắp xếp theo phân hoạch_quicksort_codehanh/////


#include <stdio.h>
#include <conio.h>
int n;
void qsort(int b[], int left, int right) {
int i, j, p, tmp, finished;
if (right>left)
{
i = left;
j = right;
p = b[left];
finished = 0;
while (!finished)
{
do
{
++i;
} while ((b[i] <= p) && (i <= right));
while ((b[j] >= p) && (j>left))
{
--j;
}
if (j<i)
finished = 1;
else
{
tmp = b[i];
b[i] = b[j];
b[j] = tmp;
}
}
tmp = b[left];
b[left] = b[j];
b[j] = tmp;
qsort(b, left, j - 1);
qsort(b, i, right);
}
return;
}

int main()
{
int a[100], i, l, r;
printf("Nhap n: ");
scanf_s("%d", &n);
printf("Nhap mang: \n");
for (i = 0; i<n; i++)
scanf_s("%d", &a[i]);
l = 0;
r = n - 1;
qsort(a, l, r);
printf("Mang sau khi duoc sap xep: ");
for (i = 0; i<n; i++)
printf("%d ", a[i]);
_getch();
return 0;
}

///// sắp xếp theo phương pháp trộn trực tiếp _mergesort/////
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<math.h>
void merge_sort(int x[], int MAX_ARY, int end, int start) {
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[100];
if (end == start)
return;
mid = (end + start) / 2;
merge_sort(x, MAX_ARY, end, mid);
merge_sort(x, MAX_ARY, mid + 1, start);
for (j = 0; j < size; j++)
executing[j] = x[end + j];
mrg1 = 0;
mrg2 = mid - end + 1;
for (j = 0; j < size; j++) {
if (mrg2 <= start - end)
if (mrg1 <= mid - end)
if (executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++]; else
x[j + end] = executing[mrg1++]; else
x[j + end] = executing[mrg2++]; else
x[j + end] = executing[mrg1++];
}
}

int main(void) {
int MAX_ARY;
int n;
int ary[100];
printf("Nhap n: ");
scanf_s("%d", &n);
MAX_ARY = n;
int j = 0;
printf("Nhap mang: ");
for (j = 0; j<MAX_ARY; j++)
scanf_s("%d", &ary[j]);
merge_sort(ary, MAX_ARY, 0, MAX_ARY - 1);
printf("Mang sau khi duoc sap xep: ");
for (j = 0; j < n; j++)
printf("%d ", ary[j]);
_getch();
}

//// khử dệ quy tính giải thừa/////


#include <stdio.h>
int main() {
int n;
do {
printf("Nhap n: ");
scanf("%d", &n);
} while (n < 0);
int tich = 1;
for (int i=1; i<=n; i++)
tich = tich*i;
printf("%d! = %d", n, tich);
return 0;
}

//// dùng khử đệ quy tính fibonaci//////////


#include<conio.h>
#include <stdio.h>
int main() {
int n;
do {
printf("Nhap n: ");
scanf_s("%d", &n);
} while (n < 1);
int fibo1 = 1;
int fibo2 = 1;
int dem = 2;
while (dem < n) {
fibo2 = fibo1 + fibo2;
fibo1 = fibo2 - fibo1;
dem++;
}
printf("Fibo(%d) = %d", n, fibo2);
_getch();
return 0;
}
/////dùng khử đệ quy để tính lãi suất ngân hang//////
#include <stdio.h>
#include<conio.h>
#include<math.h>
int main() {
int n;
float s;
printf("NGAN HANG, LAI SUAT 11%%\n");
do {
printf("Nhap so tien (USD): ");
scanf_s("%f", &s);
printf("Nhap so nam: ");
scanf_s("%d", &n);
printf("-----------------------\n");
} while ((n <= 0) | (s <= 0));
int dem = 0;
while (dem < n) {
s = s*1.11;
dem++;
}
printf("So tien sau %d nam la %.1f USD", n, s);
_getch();
return 0;
}

//// dùng khử đệ quy để tính bài toán tháp hà nội////


#include<conio.h>
#include<stdio.h>
void hanoiNonRecursion(int num, char sndl, char indl, char dndl)
{
char stkn[50],stksndl[50],stkindl[50],stkdndl[50],stkadd[50], temp;
int top, add;
top = NULL;
one:
if (num == 1)
{
printf("\nChuyen dia tu cot %c sang cot %c ", sndl, dndl);
goto four;
}
two:
top = top + 1;
stkn[top] = num;
stksndl[top] = sndl;
stkindl[top] = indl;
stkdndl[top] = dndl;
stkadd[top] = 3;
num = num - 1;
sndl = sndl;
temp = indl;
indl = dndl;
dndl = temp;
goto one;
three:
printf("\nChuyen dia tu cot %c sang cot %c ", sndl, dndl);
top = top + 1;
stkn[top] = num;
stksndl[top] = sndl;
stkindl[top] = indl;
stkdndl[top] = dndl;
stkadd[top] = 5;
num = num - 1;
temp = sndl;
sndl = indl;
indl = temp;
dndl = dndl;
goto one;
four:
if (top == NULL)
return;
num = stkn[top];
sndl = stksndl[top];
indl = stkindl[top];
dndl = stkdndl[top];
add = stkadd[top];
top = top - 1;
if (add == 3)
goto three;
else if (add == 5)
goto four;
}

int main()
{
int no;
printf("Nhap so dia: ");
scanf_s("%d", &no);
if (no<1)
printf("\nKhong co dia nao.");
else
printf("Cach chuyen: ");
hanoiNonRecursion(no, 'A', 'B', 'C');
_getch();

return 0;
}
/////// dùng phương pháp đệ quy để tính thap ha noi////
#include<stdio.h>
#include<conio.h>
#include<math.h>
void thaphanoi(int n, char a, char b, char c)
{
if (n == 1)
{
printf("\t%c-----------%c\n", a, c);
return;
}
thaphanoi(n - 1, a, c, b);
thaphanoi(1, a, b, c);
thaphanoi(n - 1, b, a, c);
}
int main() {
char a = 'A', b = 'B', c = 'C';
int n;
printf("nhap n:");
scanf_s("%d", &n);
thaphanoi(n, a, b, c);
_getch();

You might also like