0% found this document useful (0 votes)
39 views9 pages

Bài xen kẽ phần tử: Chương 1

The document contains code for several algorithms: 1. A function to interleave the elements of two arrays into a third array 2. Functions to remove extra whitespace from a string and capitalize the first letter of each word 3. Functions to calculate the next date given a date and handle leap years 4. Functions to add and subtract two large numbers represented as strings 5. A function that fills a 2D array in a spiral/clockwise pattern

Uploaded by

Trần Duy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views9 pages

Bài xen kẽ phần tử: Chương 1

The document contains code for several algorithms: 1. A function to interleave the elements of two arrays into a third array 2. Functions to remove extra whitespace from a string and capitalize the first letter of each word 3. Functions to calculate the next date given a date and handle leap years 4. Functions to add and subtract two large numbers represented as strings 5. A function that fills a 2D array in a spiral/clockwise pattern

Uploaded by

Trần Duy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Chương 1

Bài xen kẽ phần tử

#include<cmath>
void GopMang(int A[], int B[], int nA, int nB);
void nhap(int A[100], int &nA);
int main()
{
int A[100];
int B[100];
int nA, nB;
nhap(A, nA);
nhap(B, nB);
GopMang(A, B, nA, nB);
system("pause");
}
void nhap(int A[100], int &nA)
{
scanf_s("%d", &nA);
for (int i = 0; i < nA; i++)
scanf_s("%d", &A[i]);
}
void xuat(int C[], int nC)
{
for (int i = 0; i < nC; i++)
printf("%d \n", C[i]);
}
void GopMang(int A[], int B[], int nA, int nB)
{
int vitri = 0;
int C[200];
int nC = 0;
if (nA > nB) // nếu A nhiều ptu hơn B
{
for (int i = 0; i < nB; i++) //xen kẽ
{
C[nC++] = A[i];
C[nC++] = B[i];
vitri = i;
}
for (int i = vitri+1; i < nA; i++) // thêm vẫn tử còn lại của A vào cuối mảng C
C[nC++] = A[i];
}
else
{
{
for (int i = 0; i < nA; i++)
{
C[nC++] = A[i];
C[nC++] = B[i];
vitri = i;
}
for (int i = vitri + 1; i < nB; i++)
C[nC++] = B[i];
}
}
xuat(C, nC);
}

Bài xóa khoảng trắng thừa


#include<stdio.h>

#include<string.h>

#include<cmath>

void huy(char A[], int vitri, int &nA);


void KhoangTrangThua(char A[], int &nA);
void xuat(char A[], int nA);
void InHoa(char A[], int nA);
void main()
{
char A[100];
int nA;
gets_s(A);
KhoangTrangThua(A,nA);
InHoa(A, nA);
xuat(A, nA);
system("pause");
}
void huy(char A[], int vitri, int &nA)
{
for (int i = vitri; i < nA - 1; i++)
A[i] = A[i + 1];
nA--;
}
void KhoangTrangThua(char A[], int &nA)
{
nA = strlen(A);

for (int i = 0; i < nA; i++)


if (A[i] == ' ' && A[i + 1] == ' ')
{
huy(A, i + 1, nA);
i--;
}
if (A[0] == ' ')
{
huy(A, 0, nA);
}
}
void InHoa(char A[], int nA)
{
if (A[0] > 'a' && A[0] < 'z')
A[0] = A[0] - 32;
for (int i = 0; i < nA; i++)
if (A[i - 1] == ' ' && A[i] > 'a' && A[i] < 'z')
A[i] = A[i] - 32;
}
void xuat(char A[], int nA)
{
for (int i = 0; i < nA; i++)
printf("%c", A[i]);
printf("\n");
}
Ngày tháng năm tiếp theo
#include<stdio.h>
#include<cmath>
void nhap(int &dd, int &mm, int &yy);
void xuat(int kq,int dd, int mm, int yy);
int xuli(int &dd, int &mm, int &yy, int A[]);
void main()
{
int A[13] = { 31,31,28,31,30,31,30,31,31,30,31,30,31 };
int dd, mm, yy;
nhap(dd, mm, yy);
int kq=xuli(dd, mm, yy,A);
xuat(kq, dd, mm, yy);
system("pause");
}
void nhap(int &dd, int &mm, int &yy)
{
scanf_s("%d%d%d", &dd, &mm, &yy);
}
void xuat(int kq, int dd, int mm, int yy)
{
if (kq == 1)
printf("%d %d %d", dd, mm, yy);
else
printf("ngay khong hop le");

}
int NamNhuan(int yy) // kiem tra nam nhuan
{
if (yy % 4 == 0 && yy % 100 == 0)
return 1;
return 0;
}
int xuli(int &dd, int &mm, int &yy,int A[])
{
if (NamNhuan(yy) == 1)
A[2] = 29;
if (dd <0 || dd> A[mm]) // TH sai
return 0;
if ( NamNhuan(yy) == 0 && dd > 28 ) //
return 0;
if (yy <= 0 || mm > 13 || mm <= 0 )
return 0;

if (dd > 0 && dd < A[mm]) // TH dung


dd++;
else if (dd == A[mm])
{
dd = 1;
if (mm > 0 && mm < 12)
mm++;
else if (mm == 12)
{
mm = 1;
yy++;
}
}
return 1;
}

Bài tính tổng hiệu 2 sô lớn


#include<stdio.h>
#include<cmath>
#include<string.h>
void TinhTong(char S1[], char S2[], char S[]);
void xuat(char S[]);
void ChuanHoaChuoi(char S1[], char S2[]);
void TinhHieu(char S1[], char S2[], char Hieu[]);
void main()
{
char S1[100], S2[100], S[100],Hieu[100];
gets_s(S1);
gets_s(S2);
ChuanHoaChuoi(S1, S2);
TinhTong(S1, S2, S);
xuat(S);
TinhHieu(S1, S2, Hieu);
_strrev(Hieu);
xuat(Hieu);
system("pause");
}

void TinhHieu(char S1[], char S2[], char Hieu[])


{
int t = strlen(S1);
int nho = 0;
_strrev(S1);
_strrev(S2);
for (int i = 0 ; i < t; i++)
{
int hieu = (S1[i] - '0') - (S2[i] - '0')-nho;
if (hieu >= 0)
{
Hieu[i] = hieu + '0';
nho = 0;
}
else if(hieu <0)
{
Hieu[i] = (S1[i]-'0') + 10-(S2[i]-'0') + '0';
nho = 1;
}
}

Hieu[t]='\0';

}
void ChuanHoaChuoi(char S1[100],char S2[])
{
int nS1 = strlen(S1);
int nS2 = strlen(S2);
_strrev(S1);
_strrev(S2);
if (nS1 > nS2)
{
int t = nS1 - nS2;
for(int i=0; i < t; i++)
S2[nS2++] = '0';
}
if (nS2 > nS1)
{
int t = nS2 - nS1;
for (int i = 0; i < t; i++)
S1[nS1++] = '0';
}
_strrev(S2);
_strrev(S1);
}

void TinhTong(char S1[], char S2[],char S[])


{
_strrev(S1);
_strrev(S2);
int l = strlen(S1);
int nho=0 ;
for (int i = 0; i < l; i++)
{
int t = (S1[i] - '0') + (S2[i] - '0')+nho;
if (t >= 10)
{
S[i] = t-10+'0';
nho = 1;
}
else
{
S[i] = t + '0';
nho = 0;
}
}
if (nho == 1)
S[l++] = '1';
S[l] = '\0';
S1[l] = '\0';
S2[l] = '\0';
_strrev(S);
_strrev(S1);
_strrev(S2);
}
void xuat(char S[])
{
int t = strlen(S);
for (int i = 0; i < t; i++)
printf("%c", S[i]);
printf("\n");
}
mảng hình xoắn ốc
#include<stdio.h>
#include<cmath>
void xuli(int A[][100], int n, int &hang, int &cot,int &x,int &dutru,int &dutru1);
void nhap(int A[][100], int &n);
void xuat(int A[][100], int n);
void main()
{
int A[100][100] = { 0 };
int n,hang,cot,x,dutru, dutru1;
nhap(A, n);
xuli(A, n,hang,cot,x ,dutru,dutru1);
xuat(A, n);
system("pause");
}
void nhap(int A[][100], int &n)
{
scanf_s("%d", &n);
}
void XoanCotLen(int A[][100], int n,int &hang,int &cot,int &x)
{

for (int j = cot; j < n - cot; j++)


{
x++;
A[hang][j] = x;
}
hang++;
cot++;
}
void XoanHangXuong(int A[][100], int n, int &hang, int &cot,int &x)
{
for (int i = hang; i <= n - hang; i++)
{
x++;
A[i][n - cot]=x; // gang A[i][j]=x;
}
cot++;
}
void XoanCotLui(int A[][100], int n, int &hang, int &cot, int &x,int &dutru)
{
for (int j = n - cot; j >= dutru; j--)
{
x++;
A[n - hang][j] = x;
}
dutru++;
}
void XoanHangLen(int A[][100],int n,int &hang,int &cot,int &x,int &dutru,int &dutru1)
{
for(int i=n-cot; i >= dutru ; i--)
{
x++;
A[i][dutru1]=x;
}
dutru1++;
cot--;
}
void xuli(int A[][100], int n,int &hang,int &cot,int &x,int &dutru,int &dutru1)
{
int dem = 0;
dutru1 = 0;
dutru = 0;
hang = 0;
cot = 0;
x = 0;
do
{
XoanCotLen(A, n, hang, cot,x);
XoanHangXuong(A, n, hang, cot,x);
XoanCotLui(A, n, hang, cot, x,dutru);
XoanHangLen(A, n, hang, cot, x,dutru,dutru1);
dem++;
} while (dem < n*n);
}
void xuat(int A[][100], int n)
{
for (int i = 0; i < n; i++)
{
{
for (int j = 0; j < n; j++)
printf("%d ", A[i][j]);
}
printf("\n");
}

}
//dutru 1 va dutru la những biến để lùi dòng thụt cột.
Bài Tìm Điểm Lòi
#include<stdio.h>
#include<stdlib.h>

void nhap(int &m, int &n, float a[][100])


{
scanf_s("%d%d", &m, &n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf_s("%f", &a[i][j]);
}
int kt(int x, int y, int X[], int Y[],float a[][100])
{
for (int i = 0; i < 5; i++)
if (a[x][y] < a[x + X[i]][y + Y[i]])
return 0;
return 1;
}
void diemloi(int &m, int &n, float a[][100],int X[],int Y[],int &nx,int X1[],int Y1[])
{
for (int i =m+1; i>0 ;i--)
for (int j = n+1 ; j >0; j--)
{
a[i][j] = a[i - 1][j - 1];
}
m += 2;
n += 2;
for (int i = 0; i < m; i++) // cot =0
{
a[i][0] = 0;
a[i][n-1] = 0;
}
for (int i = 0; i < n; i++) // hang =0
{
a[0][i] = 0;
a[m-1][i] = 0;
}
for(int i=1;i<m-1;i++)
for(int j=1;j<n-1;j++)
if (kt(i, j, X, Y, a) == 1)
{
X1[nx] = i;
Y1[nx] = j;
nx++;
}

}
void timmin(int nx,int X1[],int Y1[],float a[][100])
{
int x=0, y=0;
float min = a[X1[0]][Y1[0]];
for (int i = 0; i < nx; i++)
if (a[X1[i]][Y1[i]] < min)
{
min = a[X1[i]][Y1[i]];
x = X1[i]; //m
y = Y1[i]; //n
}
printf("%3d%3d", x-1, y-1);
}
void main()
{
int X[5] = { -1,0,1,0 };
int Y[5] = { 0,-1,0,1 };
int m, n;
int nx = 0;
int X1[100], Y1[100];
float a[100][100];
nhap(m, n, a);
diemloi(m, n, a,X,Y,nx,X1,Y1);
timmin(nx, X1, Y1, a);
system("pause");
}
Chương 3
tập con
#include<stdio.h>
#include<cmath>
void TapCon(int A[], int nA, int n);
void main()
{
int n;
int nA = 1;
int A[100] = { 0 };
scanf_s("%d", &n);
TapCon(A, nA, n);
system("pause");
}
void xuat(int A[],int nA)
{
for (int i = 0; i < nA; i++)
printf("%d", A[i]);
printf("\n");
}
void TapCon(int A[], int nA, int n)
{
int dem = 0;
int xet = 1;
xuat(A, nA);
while( xet != 0 )
{
while (dem != 1) // xet dieu kien tang duoc
{
if (A[nA - 1] == n - 1)
dem = 1;
else
{
A[nA] = A[nA - 1] + 1;
nA++;
xuat(A, nA);
}
}
if (nA > 1) // khi khong tang duoc
{
nA--;
A[nA - 1] = A[nA - 1] + 1;
xuat(A, nA);
dem = 0;
}
else
xet = 1;

}
}
Bài hoán vị
#include<stdio.h>
#include<cmath>
void HoanVi(int A[], int nA);
void nhap(int A[], int &nA);
void main()
{
int A[100], nA;
nhap(A, nA);
HoanVi(A, nA);
system("pause");
}
void nhap(int A[],int &nA)
{
scanf_s("%d", &nA);
for (int i = 0; i < nA; i++)
scanf_s("%d", &A[i]);
}
void SapXep(int &x, int &y)
{
int gang = 0;
gang = x;
x = y;
y = gang;
}
void xuat(int A[], int nA)
{
for (int i = 0; i < nA; i++)
printf("%d ", A[i]);
printf("\n");
}
void DaoMang( int A[],int nA,int vitri)
{

for (int i = vitri; i < vitri+ (nA-vitri) / 2; i++)


{
int temp = A[i];
A[i] = A[nA -1-i+vitri];
A[nA-1-i+vitri] = temp;
}
}

void HoanVi(int A[],int nA)


{
int j = nA - 1;
xuat(A, nA);
while (1)
{
while (A[j] < A[j - 1]) // tim vi tri dau tien J ma A[j] < A[j-1]
j--;
if (j > 0)
{
int i;
for (i = nA - 1; i >= j; i--)
if (A[i] > A[j - 1])
{
break; // tim duoc vi tri i dau tien de
A[i] > A[j]
}
SapXep(A[i], A[j - 1]);
DaoMang(A, nA, j);
xuat(A, nA);
}
if (j == 0)
break;
j = nA - 1; // dua J ve ve tri ben phai xet lai tu dau

}
Bài nhập số thực A ( 0< A < 4) tìn n nhỏ nhất thỏa …
#include<stdio.h>
#include<cmath>
int TimN(int &n, int A);
void xuat(int a);
int TimNC1(int n, int A, int kq);
void main()
{
int A;
int n = 0;
int kq = 0;
scanf_s("%d",&A);
int KQ=TimN(n, A);
int KQ2 = TimNC1(n, A,kq);
xuat(KQ);
xuat(KQ2);
system("pause");
}
int TimN(int &n,int A)
{
float kq = 0;
int i = 0;
while( kq < A)
{
n++;
kq = kq + float(1) / n;

}
printf("%f \n", kq);
return n;
}
// đệ quy
int TimNC1(int n,int A,int kq)
{
if (kq > A)
return n;
else
{
kq = float(1) / TimNC1(n++, A, kq);
return float(1) / TimNC1(n++, A, kq);
}
}
void xuat(int a)
{
printf("%d", a);
}

You might also like