Buoi hoc 3: De quy thap ha noi
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
/* cai dat ham de qui de tinh n!
f(n) = 0! = 1, n=0
f(n) = f(n-1)*n, n>0
*/
int giaithua(int n)
{
if(n==0)
{
return 1;
}
else
{
int k = giaithua(n-1)*n;
printf("Ham voi dau vao %d tra ve %d\n",n,k);
return k;
}
}
/* cai dat ham de qui de tinh x^n (n>=0)
f(n) = 1, n = 0
f(n) = f(n-1)*x , n > 0
*/
int power(int x, int n)
{
if(n==0)
{
return 1;
}
{
//int n = 8;
//int x = giaithua(n);
//int x = power(2,n);
//printf("\nf(%d,%d)=%d\n", 2, n, x);
//thaphanoi(4,'A','B','C');
int A[]={5,1,2,9,4,0,80, 7};
int x = timmin(A,8);
printf("%d\n", x);
getch();
return 1;
}
Buoi hoc 2: Cai dat de quy
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
/* cai dat cong thuc de qui:
f(0) = 3,
n=0
f(n+1) = 2f(n) + 3, n > 0
*/
int hamf(int n)
{
/* in ra */
printf("Bat dau goi ham voi dau vao la %d\n",n);
if(n==0)
{
printf("Ham fibo voi n=%d\n",n);
if(n < 2) return n;
else return Fibo(n-1) + Fibo(n-2);
}
int A[10];
int Fibowmem(int n)
{
printf("Ham fibo voi n=%d\n",n);
if(n < 2) return n;
else
{
if(A[n-1]==-1) A[n-1] = Fibowmem(n-1);
if(A[n-2]==-1) A[n-2] = Fibowmem(n-2);
return A[n-1] + A[n-2];
}
}
/* vi du don gian backtrack */
int C[] = {1,2,3,4};
int D[] = {5,6,7,8};
int kq[] = {0,0};
int UCV(int C[],int D[],int i,int k,int kq[])
{
if(k==1) return 1;
if(k==2){
if((D[i]+kq[0])==10) return 1;
else return 0;
}
return 0;
backtrack_vidu1(1);
getch();
return 1;
}
Buoi hoc 5p1: Quay lui Hien thi hoan vi
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
int A[]={1,2,3};
int kq[]={0,0};
int n;
void Inkq()
{
int i;
printf(" kq:");
for(i=0;i
void Inkq()
{
int i;
printf(" kq:");
for(i=0;i
{
return (lp->last +1);
}
void insert (elementtype x , int p , list_type * lp)
{ //int v; // running position
if (lp -> last >= maxlength - 1)
{
printf("\n%s","list is full");
return;
}
if ((p < 0) || (p > lp -> last + 1))
{
printf("\n%s ","position does not exist");
return;
}
else {
for (int q = lp -> last; q >= p; q--)
lp -> elements [q+1] = lp -> elements [q];
lp -> last = lp -> last +1 ;
lp -> elements[p] = x;
}
}
void deleteL(int p , list_type * lp)
{
int q; /* running position */
if ((p > lp-> last) || (p < 0))
{
printf("\n%s ","position does not exist");
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
typedef int ElementType;
struct NodeType{
ElementType Inf;
NodeType *Next;
};
typedef struct NodeType LIST;
void duyet(LIST *lp)
{
LIST *tg;
tg = lp;
printf(" \n Ds dang duyet: ");
while(tg != NULL)
{
printf(" %d ", tg->Inf);
tg = tg->Next;
}
}
void InsertToLast(LIST *lp, ElementType X)
{
if(lp==NULL) return;
LIST *nutx;
nutx = (LIST*)malloc(sizeof(LIST));
nutx->Inf = X;
nutx->Next = NULL;
LIST *tg;
free(lp);
//lp = tempprt;
//return lp;
return tempprt;
}
void DeleteAtLast(LIST *lp)
{
LIST *tg, *tg_old;
if((lp==NULL)||(lp->Next==NULL)) return;
tg = lp;
tg_old;
while(tg->Next != NULL)
{
tg_old = tg;
tg = tg->Next;
}
free(tg);
tg_old->Next=NULL;
}
int main()
{
LIST *danhsach3=NULL;
danhsach3 = InsertToHead(danhsach3, 1);
InsertToLast(danhsach3, 3);
Insert_Middle(danhsach3, 2);
danhsach3=DeleteAtHead(danhsach3);
//printf("\n -- danh sach khoi tao ban dau -- \n");
//duyet(danhsach3);
if (s == NULL)
{
return NULL; // No memory
}
s->top = NULL;
return s;
}
/*** Kiem tra Stack rong ***/
int StackEmpty(const Stack *s)
{
return (s->top == NULL);
}
/*** Thông báo Stack tràn ***/
int StackFull()
{
printf("\n NO MEMORY! STACK IS FULL");
getch();
return 1;
}
int StackPush(Stack *s, eletype item)
{
StackNode *node;
node = (StackNode*)malloc(sizeof(StackNode)); //(1)
if (node == NULL)
{
StackFull();
return 1; // Tràn Stack: het bo nho
}
if((in=='{')||(in=='[')||(in=='('))
return 1;
else return 0;
}
int ktracungkieu(char a, char b)
{
if(a=='{')
if(b=='}') return 1;
else return 0;
if(a=='[')
if(b==']') return 1;
else return 0;
if(a=='(')
if(b==')') return 1;
else return 0;
}
int kiemtrangoacdung(Stack *s, char in[], int n)
//(1)
{
int i;
for(i=0; i
/* Cai dat hang doi su dung ds noi don */
typedef int eletype;
struct QueueNode {
eletype item;
QueueNode *Next;
};
typedef struct _Queue
{
QueueNode *front, *back;
} Queue;
Queue *QueueConstruct()
{
Queue *q;
q = (Queue *)malloc(sizeof(Queue));
if (q == NULL) {
return NULL; // No memory
}
q->front = NULL;
q->back = NULL;
return q;
}
/*** Ki?m tra Stack r?ng ***/
int QueueEmpty(const Queue *q)
{
return (q->front == NULL);
}
/*** Thông báo Stack tràn ***/
int QueueFull()
{
eletype item;
QueueNode *node;
if (QueueEmpty(q))
{
//(1)
// Empty Queue, can't dequeue
return 0;
}
node = q->front;
item = node->item;
if((q->front==q->back)&&(q->front!=NULL))
{
q->front = NULL;
q->back = NULL;
free(node);
}
else
{
q->front=node->Next;
free(node);
}
return item;
}
int main()
{
Queue *q = QueueConstruct();
EnQueue(q,2);