HKI - LTCB Tuần 5
Bài 1:
Bài 2:
/*
Ten: Hoang Minh Tuan
Lop: GS19TH1
*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 100
// Nguyen Mau Ham
void Nhap(int a[], int &n);
void Xuat(int a[], int n);
void Tim_Vi_Tri(int a[], int n, int &x);
void Liet_Ke_Vi_Tri_Bang_X(int a[], int n, int x);
void Xoa_Phan_Tu_Tai_Vi_Tri(int a[], int &n);
void Liet_Ke_Phan_Tu_Chi_Het_Cho_2(int a[], int n);
void Liet_Ke_Cac_Cap_XY(int a[], int n);
void Liet_Ke_Phan_Tu_Lon_Hon_Phan_Tu_Lien_Sau(int a[], int n);
void Tach_Mang(int a[], int b[], int c[], int &n, int &nb, int &nc);
// Ham main
int main(){
int a[MAX], b[MAX], c[MAX], n, nb, nc, x;
printf("========================= NHAP MANG =======================\n"); Nhap(a,n);
printf("========================= XUAT MANG =======================\n\n"); Xuat(a,n);
printf("\n\n================= TIM TRA VE VI TRI DAU TIEN ===============\n\n"); Tim_Vi_Tri(a,n,x);
printf("\n\n================= LIET KE VI TRI CAN TIM ===============\n\n"); Liet_Ke_Vi_Tri_Bang_X(a,n,x);
printf("\n\n================= LIET KE VI TRI CAN TIM ===============\n\n"); Xoa_Phan_Tu_Tai_Vi_Tri(a,n); Xuat(a,n);
printf("\n\n================= LIET KE PHAN TU CHIA HET CHO 2 ===============\n\n"); Liet_Ke_Phan_Tu_Chi_Het_Cho_2(a,n);
printf("\n\n================= LIET KE CAC CAP X <= Y ===============\n"); Liet_Ke_Cac_Cap_XY(a,n);
printf("\n\n================= LIET KE CAC PHAN TU LON HON ===============\n\n"); Liet_Ke_Phan_Tu_Lon_Hon_Phan_Tu_Lien_Sau(a,n);
printf("\n\n================= TACH MANG ===============\n\n"); Tach_Mang(a,b,c,n,nb,nc);
_getch();
return 0;
}
// Dinh Nghia Ham
void Nhap(int a[], int &n){
printf("Nhap So Luong Phan Tu: "); scanf("%i", &n);
for(int i=0; i<n; i++){
printf("Nhap Phan Tu Thu [%i]: ", i+1); scanf("%i", &a[i]);
}
}
void Xuat(int a[], int n){
for(int i=0; i<n; i++){
printf("Phan Tu Thu [%i]: %i\n", i+1, a[i]);
}
}
void Tim_Vi_Tri(int a[], int n, int &x){
int m= 0, count= 0;
printf("Nhap Phan Tu Can Tim: "); scanf("%i", &x);
for(int i=0; i<n; i++){
if(a[i] == x){
m= 1;
count= i;
}
}
if(m == 1) printf("Tim Thay X Tai Vi Tri: %i", count+1);
else printf("Khong Tim Thay %i Trong Mang", x);
}
void Liet_Ke_Vi_Tri_Bang_X(int a[], int n, int x){
int count= 0, vitri= 0;
printf("Vi Tri Trung Nhau: ");
for(int i=0; i<n; i++){
if(a[i] == x){
count++;
printf("%i ", i+1);
vitri= i;
}
}
printf("\nSo Luong Trung Nhau: %i",count);
}
void Xoa_Phan_Tu_Tai_Vi_Tri(int a[], int &n){
int vtri;
printf("Nhap Vao Vi Tri Can Xoa: "); scanf("%i", &vtri);
if(vtri >= 1 && vtri <= n){
for(int i=vtri-1; i<n-1; i++){
a[i]= a[i+1];
}
n--;
}
}
void Liet_Ke_Phan_Tu_Chi_Het_Cho_2(int a[], int n){
for(int i=0; i<n; i++){
if(a[i] % 2 == 0) printf("%i ", a[i]);
}
}
void Liet_Ke_Cac_Cap_XY(int a[], int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(a[i] <= a[j]) printf("\n( %i %i )", a[i], a[j]);
}
}
}
void Liet_Ke_Phan_Tu_Lon_Hon_Phan_Tu_Lien_Sau(int a[], int n){
for(int i=0; i<n; i++){
if(a[i] > a[i+1]) printf("%i %i\t", a[i], a[i+1]);
}
}
void Tach_Mang(int a[], int b[], int c[], int &n, int &nb, int &nc){
int f=0, z= 0;
for(int i=0; i<n; i++){
if(a[i] % 2 == 0) b[f++]= a[i];
else c[z++]= a[i];
}
nb= f;
nc= z;
printf("Mang So Chan\n"); Xuat(b,nb);
printf("\nMang So Le\n"); Xuat(c,nc);
}
Bai 3:
No Comment to " HKI - LTCB Tuần 5 "