Tải bản đầy đủ (.doc) (35 trang)

thảo luận hệ thống thông tin quản lý cho mảng 50 pt số nguyên, viết phương trình gồm hàm nhập, sắp xếp, tìm pt theo yêu cầu

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (174.99 KB, 35 trang )

Câu 1:
Đề 01: cho mảng 50 pt số nguyên, viết phương trình gồm hàm nhập, sắp xếp, tìm pt
theo yêu cầu sau:
- Nhập mảng
- Sắp xếp theo kiểu chèn tìm các pt âm và in ra vị trí của nó trong mảng
 Đánh giá độ phức tạp của thuật toán: nhỏ nhất, lớn nhất.
//Mang 1 chieu toi da 50 pt sap xep chen, tim va tin ra vt phan tu am
//In ra cac vi tri le xong moi sap xep
#include<stdio.h>
#include<conio.h>
void NhapMang(int a[],int &n)
{ printf("Nhap so phan tu cua mang: ");
scanf("%d",&n);
for(int i=0; i<n; i++)
{ printf("Nhap phan tu thu %d: ",i+1);
scanf("%d",&a[i]);
}
}
void XuatMang(int a[], int n)
{ printf("Noi dung cua mang la: ");
for(int i=0; i<n; i++)
printf("%d ",a[i]);
printf("\n");
}
1
//In
void TimPTAm(int a[], int n)
{ int dem=0;
for(int i=0; i<n; i++)
{ if(a[i]<0)
{ printf("Phan tu: %d, o vi tri: %d\n",a[i],i+1);


dem++;
}
}
if(dem==0) printf("Khong co phan tu am");
}
//Sap xep chen
void sxchen(int a[],int n)
{ int i,j,k,temp;
for(i=1;i<n;i++)
{ temp=a[i];
j=i-1;
while(a[j]>temp && j>=0)
{ a[j+1]=a[j];
j ;
}
a[j+1]=temp;
}
}
2
int main()
{ int a[50],n;
NhapMang(a,n);
XuatMang(a,n);
printf("Cac phan tu am: \n");
TimPTAm(a,n);
sxchen(a,n);
printf("Sau khi duoc xap xep. ");
XuatMang(a,n);
getch();
return 0;

}
/* Do phuc tap:
1. Sx chèn:
TH1: Tot nhat mang da sap xep tang
Nhu vay ta tim ngay duoc vi tri thich hop de chen dau tien ma ko can vao vong lap
i chay tu 1 den n-1 so phep so sanh la n-1
So phep gan: Gmin=2(n-1) (2 phep gan thuat toan ko chay vao vong lap j)
Do phuc tap: O(n)
TH2: Xau nhat Vi tri chen luon la vi tri dau tien cua day da co thu tu
So phep so sanh la: (n-1) + (n-2) + +1 = n(n-1)/2
So phep gan la: Gmax=Gmin+ =2(n-1) + Smin.1=2(n-1) + n(n-1)/2
Do phuc tap: O(n^2)
3
2. Sx nổi bọt:
void sxnoibot(int a[],int n)
{ int i,j,temp;
for(i=0;i<n;i++)
for(j=n-1;j>i;j )
if(a[i]>a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
• Độ phức tạp:
TH1: Tot nhat la mang da co thu tu tang
So phep hoan vi: Hmin=0;
TH2: Xau nhat la mang co thu tu giam
Lan thu nhat duyet khoang n-1 phep so sanh, lan thu 2 duyet khoang n-2 phep so sanh
So phep hoan vi: Hmin=(n-1) + n(n-2) + + 2 +1 =n(n-1)/2

So phep hoan vi trung binh: Htb=n(n-1)/4
Vay do phuc tap cua thuat toan la: O(n^2)
3. Sx chọn:
void sxchon(int a[],int n)
{ for(int i=0;i<n-1;i++)
{ int min=i;
for(int j=i+1;j<n;j++)
if(a[j]<a[min]) min=j;
int temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
• Độ phức tạp
Trong moi truong hop:
So phep so sanh la: S=(n-2)+(n-1)+ +2+1=n(n-1)/2
So phep hoan vi: n-1 lan
4
TH1: TH tot nhat mang da duoc sap xep roi
So phep gan Gmin=2(n-1)
TH2: Mang co thu tu giam dan
So phep gán: Gmax=2[n+ (n-1) + +1]=n(n+1)
So phep gan trung binh: Gtb=n-1 + n(n+1)/2
Do phuc tap: O(n^2)
4. Sx nhanh
void sxnhanh(int a[],int left,int right)
{ int i,j,x;
i=left;
j=right;
x=a[left];

do
{ while(a[i]<x && i<right)
i++;
while(a[j]>x && j>left)
j ;
if(i<=j)
{ int temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j ;
}
}
while(i<=j);
if(i<right)
sxnhanh(a,i,right);
if(j>left)
sxnhanh(a,left,j);
}
5
Đề 3: cho 1 mảng số nguyên gồm tối đa 50 pt. Viết chương trình trong đó XĐ các
hàm có chức năng nhập, sắp xếp, tìm kiếm và in ra kết quả để:
- Nhập mảng
- Sắp xếp mảng theo thuật toán chèn
- Tìm và in ra giá trị và vị trí của pt đầu tiên trong mảng mà chia hết cho 5
 Đánh giá độ phức tạp của thuật toán trong các trường hợp tốt nhất, xấu nhất
// Do phuc tap:
TH1: Tot nhat mang da sap xep tang
Nhu vay ta tim ngay duoc vi tri thich hop de chen dau tien ma ko can vao vong lap
i chay tu 1 den n-1 so phep so sanh la n-1

So phep gan: Gmin=2(n-1) (2 phep gan thuat toan ko chay vao vong lap j)
Do phuc tap: O(n)
TH2: Xau nhat Vi tri chen luon la vi tri dau tien cua day da co thu tu
So phep so sanh la: (n-1) + (n-2) + +1 = n(n-1)/2
So phep gan la: Gmax=Gmin+ =2(n-1) + Smin.1=2(n-1) + n(n-1)/2
Do phuc tap: O(n^2)
Mảng 2 chiều:
#include <conio.h>
#include <iostream.h>
#define max 100
typedef int matran[max][max];
matran a;
void nhap(matran a,int m,int n)
//m :hang; n:cot
{ int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ cout<<"Nhap phan tu a["<<i<<"]["<<j<<"] = ";
cin>>a[i][j];
}
}
void xuat(matran a,int m,int n)
{ int i,j;
6
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{cout<<a[i][j]<<" ";}
cout<<"\n";}
}
void timptu(matran a,int m,int n,int x)

//tim vitri phan tu co gia tri x
{ int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ if(a[i][j]==x)
cout<<"Phan tu "<<x<<" thuoc hang " <<i<<"cot"<<j;
}
}
int tongcotchan(matran a,int m,int n)
//tong phan tu o cot chan
{ int i,j,s=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ if(j%2==0)
s=s+a[i][j];
}
return s;
}
int demspt(matran a,int m,int n)
//dem so phan tu chia het cho 3
{ int i,j,dem=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ if(a[i][j]%3==0)
dem++;
}
}
int sapxepnoibottheohang(matran a,int x,int n)
//sapxep giam dan noi bot hang x
{ int i,j; //hang x -> i=x-1

for(i=0;i<n-1;i++)
for(j=n;j>i;j )
if(a[x-1][j]>a[x-1][j-1])
{ int temp=a[x-1][j-1];
a[x-1][j-1]=a[x-1][j];
a[x-1][j]=temp;
}
7
}
void sapxepchontheohang(matran a,int x,int n)
//sapxep chon theo hang x
{ int i,j,m,temp; //hang x -> i=x-1
for(i=0;i<n-1;i++)
{ m=i;
for(j=i+1;j<n;j++)
if(a[x-1][j]<a[x-1][m])
m=j;
temp=a[x-1][i];
a[x-1][i]=a[x-1][m];
a[x-1][m]=temp;
}
}
void sxchen(matran a,int x,int n)
//sx chen tang dan theo hang x
{ int i,j,temp;
for(i=1;i<n;i++)
{ temp=a[x-1][i];
j=i-1;
while(a[x-1][j]>temp && (j>=0))
{ a[x-1][j+1]=a[x-1][j];

j ;
}
a[x-1][j+1]=temp;
}
}
int main()
{ int m,n,x;
cout<<"Nhap so hang: ";
cin>>m;
cout<<"Nhap so cot: ";
cin>>n;
nhap(a,m,n);
cout<<"Nhap hang can sapxep: ";
cin>>x;
sapxepnoibottheohang(a,x,n);
sapxepchontheohang(a,x,n);
sxchen(a,x,n);
xuat(a,m,n);
getch();
}
8
Câu 2:
Đề 01: viết trương trình tạo một hàng đợi với các thao tác ktra rong, PUSH, POP,
ktao
//Cai dat hang doi bang dslk
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int item;

struct node *next;
};
typedef struct node *queuenode;
typedef struct
{ queuenode head;
queuenode tail;
}queue;
void QueueInitialize(queue *q)
{ q->head=q->tail=NULL;
return;
}
int QueueEmpty(queue q)
{ return (q.head==NULL);}
void Put(queue *q, int x)
{ queuenode p;
p=(queuenode) malloc (sizeof(struct node));
p->item=x;
p->next=NULL;
9
if(q->tail==NULL) q->tail=p;//Trong TH hang doi rong
else
{ q->tail->next =p;
q->tail=q->tail->next;
}
if(q->head==NULL) q->head=q->tail; //Trong TH hang doi rong
return;
}
int Get(queue *q)
{ queuenode p;
if(QueueEmpty(*q)) printf("Hang doi rong");

else
{ p=q->head;
q->head=q->head->next;
return p->item;
}
}
int main()
{ queue q;
int n, x, tong=0;
QueueInitialize(&q);
printf("Nhap so luong phan tu muon them: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{ printf("Nhap phan tu thu %d: ", i+1);
scanf("%d", &x);
10
Put(&q,x);
}
// In ra man hinh
printf("Danh sach cac phan tu la: ");
while(!QueueEmpty(q))
{ printf("%d ", Get(&q));}
getch();
return 0;
}
• Cài đặt hàng đợi bằng mảng:
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define max 100

typedef struct
{ int data[max];
int head;
int tail;
} queue;
void khoitao(queue &q)
{ q.head=0;
q.tail=-1;
}
int ktrong(queue &q)
{ return (q.tail==-1);}
int ktday(queue &q)
{ return(q.tail==max-1);}
void them(queue &q,int x)
{ if(ktday(q)) cout<<"Hang doi day! \n";
else
{ q.tail++;
q.data[q.tail]=x;
}
}
void bot(queue &q)
{ if(ktrong(q)) cout<<"Hang doi rong! \n";
else
11
{ if(q.head==q.tail)
khoitao(q);
else
q.head=q.head+1;
}
}

void xuat(queue &q)
{ queue p;
p=q;
if(ktrong(p)) cout<<"Hang doi rong! \n";
else
{ while(p.head!=p.tail+1)
{ cout<<p.data[p.head]<<" ";
p.head=p.head+1;
}
}
}
void nhap(queue &q)
{ int x;
while(x!=-1)
{ cout<<"Nhap nut, -1 de ket thuc: ";
cin>>x;
if(x!=-1)
them(q,x);
}
}
int main()
{ queue q;
int x,a,k;
khoitao(q);
cout<<"Nhap du lieu cho hang doi: ";
nhap(q);
xuat(q);
cout<<"Lay phan tu o cuoi ngan xep: \n";
bot(q);
cout<<"Nhap phan tu can them: ";

cin>>x;
them(q,x);
xuat(q);
getch();
}
12
Đề 02: ngăn xếp: tạo một ngăn xếp với các công việc: khởi tạo, kiểm tra rỗng,
them và bớt
//Cai dat ngan xep bang dslk cac pt so nguyen
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int item;
struct node *next;
};
typedef struct node *stacknode;
typedef struct
{ stacknode top;}stack;
void StackInitialize(stack *s)
{ s->top=NULL;}
int StackEmpty(stack s)
{ return (s.top==NULL);}
void Push(stack *s, int x)
{ stacknode p;
p=(stacknode) malloc (sizeof(struct node));
p->item=x;
p->next=s->top;
s->top=p;
return;

}
int Pop(stack *s)
{ stacknode p;
13
if(StackEmpty(*s)) printf("Ngan xep rong");
else
{ p=s->top;
int x=p->item;
s->top=s->top->next;
return x;
}
}
int main()
{ stack s;
int n, x;
StackInitialize(&s);
printf("Nhap so luong phan tu muon them: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{ printf("Nhap phan tu thu %d: ", i);
scanf("%d", &x);
Push(&s,x);
}
// In ra man hinh
printf("Danh sach cac phan tu la: ");
while(!StackEmpty(s))
{ printf("%d ", Pop(&s));}
getch();
return 0;
}

14
• Cài đặt ngăn xếp bằng mảng:
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define max 100
struct stack
{ int a[max];
int top;
};
void khoitao(stack &s)
{ s.top=-1;}
int ktrong(stack &s)
{ return(s.top==-1);}
int ktday(stack &s)
{ return(s.top==max-1);}
void them(stack &s,int x)
{ if(ktday(s)) cout<<"Stack day";
else
{ s.top++;
s.a[s.top]=x;
}
}
void bot(stack &s)
{ int x;
if(ktrong(s)) cout<<"Stack rong";
else
{ x=s.a[s.top];
s.top ;
}

}
void nhap(stack &s)
{ int x;
while(x!=-1)
{ cout<<"Nhap phan tu, -1 de ket thuc: ";
cin>>x;
if(x!=-1) them(s,x);
}
}
void xuat(stack &s)
{ stack temp=s;
while (temp.top!=-1)
{ cout<<temp.a[temp.top]<<"\n";
15
temp.top ;
}
cout<<"Hien thi stack thanh cong! ";
}
int main()
{ int x,a,k;
stack s;
khoitao(s);
cout<<"Nhap du lieu cho stack: \n";
nhap(s);
xuat(s);
cout<<"Lay phan tu o dinh ngan xep:\n";
bot(s);
xuat(s);
getch();
}

Đề : viết chương trình trong đó xác định các hàm tạo danh sách, chèn pt vào danh
sách, in danh sách để thực hiện các công việc sau:
- Tạo 1 danh sách liên kết đơn gồm các pt là số nguyên
- Chèn 1 pt có giá trị = k nhập từ bàn phím vào sau pt có giá trị lớn nhất trong
danh sách
- In ra màn hình danh sách sau khi tạo và chèn
//Dslk don gom cac pt la so nguyen. Chen vao sau pt co gia tri lon nhat trong danh sach.
In ra danh sach sau khi tao va chen
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int item;
struct node *next;
};
typedef struct node *listnode;
void Insert_Begin(listnode *p, int x)
16
{ listnode q;
q=(listnode)malloc(sizeof(struct node));
q->item=x;
q->next=*p;
*p=q;
}
//Ham TimMax tra ve con tro cua phan tu max trong dslk
listnode TimMax(listnode p)
{ listnode r;
r=p;
int max=r->item;
for(r=r->next; r!=NULL; r=r->next)

if(r->item>max) max=r->item;
r=p;
while(r!=NULL)
{ if(r->item==max) return r;
r=r->next;
}
return NULL;
}
void Insert_Max(listnode *p, listnode r, int x) //Chen vao sau nut r(max)
{ r=TimMax(*p);
listnode q;
q=*p;
if(r==NULL) printf("Them that bai");
else
17
{ q=(listnode)malloc(sizeof(struct node));
q->item=x;
q->next=r->next;
r->next=q;
}
}
void In(listnode p)
{ listnode r;
r=p;
while(r!=NULL)
{ printf("%d ",r->item);
r=r->next;
}
}
int main()

{ listnode p,r;
p=NULL; //Nho
int n,x;
printf("Nhap so luong phan tu muon them: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{ printf("Nhap phan tu thu %d: ",i+1);
scanf("%d", &x);
Insert_Begin(&p,x);
}
//In ra man hinh
18
printf("Danh sach cac phan tu la: ");
In(p);
printf("\nNhap phan tu muon them vao sau phan tu max: ");
scanf("%d", &x);
Insert_Max(&p,r,x);
printf("Danh sach sau khi them: ");
In(p);
getch();
return 0;
}
19
Câu 3:
1. Cây nhị phân tìm kiếm
Đề 01: cho dãy số nguyên: 10 3 11 4 22 7
A, vẽ cây nhị phân
B, viết chương trình thảo mãn những đk sau:
- Tạo cây nhị phân
- Tính tổng và in ra số nút >10

- Tính tổng giá trị của các phần tử nhập từ bàn phím
- Tìm và in ra giá trị các phần tử lớn hơn x
A,
B, //Cay nhi phan tim kiem. Tinh tong so nut > 10
//In ra so nut > 10
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int item;
struct node* left;
struct node* right;
};
typedef struct node* bistree;
20
bistree root;
//Khoi tao
bistree treelnit(bistree &root)
{ root=NULL;
return root;
}
//Tao moi mot nut
bistree create_node(int x)
{ bistree root= new node;
if(root!=NULL)
{ root->left=NULL;
root->right=NULL;
root->item=x;
}
return root;

}
//Them 1 nut
bistree addnode(bistree &root, int x)
{ bistree NewNode=create_node(x);
if(NewNode==NULL) return NewNode;//x chua nhap vao
if(root==NULL) root=NewNode; //cay rong thi nut goc bang NewNode
else
{ if(root->item>=x) addnode(root->left,x);
else addnode(root->right,x);
}

21
return NewNode;
}
//Duyet cay thu tu truoc
void NLR(bistree root)
{ if(root!=NULL)
{ printf("%d ",root->item);
NLR(root->left);
NLR(root->right);
}
}
//Duyệt cây thứ tự giữa
void LNR(bistree root)
{ if(root!=NULL)
{ LNR(root->left);
printf("%d ",root->item);
LNR(root->right);
}
}

//Duyệt cây thứ tự sau
void LRN(bistree root)
{ if(root!=NULL)
{ LRN(root->left);
LRN(root->right);
printf("%d ",root->item);
}
}
22
//Tinh tong so nut > 10
int bistree_tong10(bistree root)
{ If(root!=NULL)
{ int a=bistree_tong10(root->left);
int b=bistree_tong10(root->right);
if(root->item > 10) return a + b + 1;
return a+b;
}
return 0;
}
//In ra so nut > 10
void timpt(bistree root)
{ if(root!=NULL)
{ if(root->item > 10)
printf("%d ",root->item);
timpt(root->left);
timpt(root->right);
}
}
//Tinh tong gia tri cac phan tu
int bistree_tong(bistree root)

{ if(root!=NULL)
{ int a=bistree_tong(root->left);
int b=bistree_tong(root->right);
return a + b + root->item;
return a+b;
23
}
return 0;
}
//Tim va in ra gia tri cac phan tu lon hon x
//Dem phan tu lon hon x
int demptx(bistree root, int x)
{ if(root!=NULL)
{ int a=demptx(root->left, x);
int b=demptx(root->right, x);
if(root->item > x ) return a + b + 1;
return a+b;
}
return 0;
}
//In ra phan tu lon hon x
void timptx(bistree root, int x)
{ if(root!=NULL)
{ if(root->item > x)
printf("%d ",root->item);
timptx(root->left, x);
timptx(root->right, x);
}
}
int main()

{ int n, x;
treelnit(root);
24
printf("Nhap so nut cua cay: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{ printf("Nhap nut thu %d: ",i+1);
scanf("%d",&x);
addnode(root,x);
}
printf("\nIn ra cac phan tu cua cay: ");
NLR(root);
LNR(root);
LRN(root);
printf("\nSo nut >10 cua cay: %d", bistree_tong10(root));
printf("\nCac phan tu co gia tri lon hon 10 la: ");
timpt(root);
printf("\nTong gia tri cac phan tu cua cay: %d", bistree_tong(root));
printf("\nNhap phan tu x: "); scanf("%d", &x);
if(demptx(root,x)==0) printf("Khong co phan tu lon hon x");
else
{ printf("Cac phan tu co gia tri lon hon x la: ");
timptx(root,x);
}
getch();
return 0;
}
25

×