You are on page 1of 21

S nguyn t.

int songto(int n)
{
if(n<=1)
return 0;
for(int i=2;i<n;i++)
if(n%i==0)
return 0;
return 1;
}
bool KTSNT(int x)
{
if(x<2)
return false;
for(int i=2; i<=x/2; i++)
if(x%i==0)
return false;
return true;
}
S hon thin
int sohoanthien(int n)
{
int S=0;
for(int i=1;i<n;i++)
if(n%i==0)
S+=i;
if(n==S)
return 1;
return 0;
}
S hon ho
bool SOHOANHAO(int n)
{
int tong=0;
if(n<1)
return false;
else
{
for(int i=1;i<(n/2)+1;i++)
{
if(n%i==0)
tong+=i;
}
if(tong==n)
return true;

else
return false;
}
S chnh phng
int sochinhphuong(int n)
{
int a=sqrt(n);
if(a*a==n)
{
return 1;
}
return 0;
}
i ch
void doicho(int &a, int &b)
{
int tam;
tam=a;
a=b;
b=tam;
}
Xp dng
void xepdong(int a[][100], int row, int colum)
{
for(int i=0;i<row;i++)
for(int j=0;j<colum;j++)
{
for(int k=j+1;k<colum;k++)
if(a[i][j]>a[i][k])
{
doicho(a[i][j],a[i][k]);
}
}
xuat(a,row,colum);
}
Xp ct
void xepcot(int a[][100], int row, int colum)
{
for(int j=0;j<ct;j++)
for(int i=0;i<hng;i++)
{
for(int k=i+1;k<hng;k++)
if(a[i][j]>a[i][k])
{
doicho(a[i][j],a[i][k]);

}
}
xuat(a,row,colum);
}
Tng ng cho chnh
void tongduongcheochinh(int a[][100], int hang, int cot)
{
int tdc=0;
for (int i=0;i<hang;i++)
for (int j=0;j<cot;j++)
if (i==j)
{
tdc=tdc+a[i][j];
}
cout<<"\nTong Cac So Tren Duong Cheo Chinh La :="<<tdc;
cout<<"\n";
}
Tng ng cho ph.
void duongcheophu(int a[][100],int hang,int cot)
{
int s=0;
for(int i=0;i<hang;i++)
for(int j=0;j<cot;j++)
if(j==hang-1-i)
s+=a[i][j];
if(cot==hang)
cout<<"\nTong duong cheo phu la: "<<s;
else cout<<"Khong phai mtvuong";
}
Bi xp dng, ct
#include<iostream.h>
#include<conio.h>
int a[3][3], b[3][3];
int i,j,t,k;
void xepdong()
{
cout<<"mang da sap xep theo dong la:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
for(k=j+1;k<3;k++)
if(a[i][j]>a[i][k])
{
t=a[i][j];
a[i][j]=a[i][k];
a[i][k]=t;
}
for(j=0;j<3;j++)

cout<<a[i][j]<<"\t";
cout<<endl<<endl;
}
}
void xepcot()
{
cout<<"ma tran da dc xep theo cot la :\n";
for(j=0;j<3;j++)
for(i=0;i<2;i++)
for(k=i+1;k<3;k++)
if(b[i][j]>b[k][j])
{
t=b[i][j];
b[i][j]=b[k][j];
b[k][j]=t;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<b[i][j]<<"\t";
cout<<endl<<endl;
}
}
void main()
{
nhapmang();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=a[i][j];
xuat();
xepdong();
xepcot();
}
Tm v tr x trong mng
void timxtrongmang(int a[][100],int dong, int cot, int x) //Tim so lon nhat
{
int k,l,f;
f=0;
for(int i=0;i<d;i++)
for(int j=0;j<c;j++)
if(a[i][j]==x)
{
k=i+1;
l=j+1;
f=1;
}
if(f==1)
cout<<"X co trong mang; Vi tri cua X la: Dong "<<k<<" Cot "<<l;
else

cout<<"X ko co trong mang.";


}
Ghp mng gim t hai mng tng.
void GhepMangGiam_2MangTang(int a[], int na, int b[],
int nb, int c[], int &nc)
{
int i=na-1, j=nb-1;
for( ; i>=0 && j>=0; )
if(a[i]>b[j])
c[nc++]=a[i--];
else
c[nc++]=b[j--];
while(i>=0)
c[nc++]=a[i--];
while(j>=0)
c[nc++]=b[j--];
}
Ngu nhin tng
void NgauNhienTang(int a[], int n)
{
srand((unsigned)time(NULL));
a[0]=rand()%100;
for(int i=1; i<n; i++)
a[i]=a[i-1]+rand()%100+1;
}
Nhp phn t mng, trng th nhp li
void NhapBangTay_TrungNhapLai(int a[], int n)
{
int i, j;
for( i=0; i<n; i++)
{
cout<<"Nhap phan tu thu a["<<i<<"] = ";
cin>>a[i];
for( j=0; j<i; j++)
do
{
if(a[i]==a[j])
{
cout<<"Nhap trung. Nhap lai. ";
cout<<"Nhap phan tu thu a["<<i<<"] = ";
cin>>a[i];
j=0;
}
}while(a[i]==a[j]);
}
}
Nhp s nguyn dng, nh hn 0 nhp li phn t.

void Input_Matrix(int a[][100], int d, int c)


{
int i, j;
for(i=0; i<d; i++)
{
for(j=0; j<c; j++)
{
cout<<"Nhap pt ["<<i<<"]["<<j<<"]:";
do
{
cin>>a[i][j];
if(a[i][j]<1)
cout<<"Nhap lai pt";
}while(a[i][j]<1);
}
}
}
Ma trn ngu nhin
void Input_Matrix_Random(int a[][100], int d, int c)
{
srand((unsigned)time(NULL));
for(int i=0; i<d; i++)
for(int j=0; j<c; j++)
a[i][j]=rand()%100;
}
Max, min.
void maxmin(int a[][100], int hang, int cot)
{
int i,j;
int max=a[0][0];
int min=a[0][0];
for (i=0;i<hang;i++)
for (j=0;j<cot;j++)
{
if (a[i][j]>max)
max=a[i][j];
if (a[i][j]<min)
min=a[i][j];
}
cout<<"\nMax trong ma tran la: "<<max;
cout<<"\nMin trong ma tran la: "<<min;
for (i=0;i<hang;i++)
for (j=0;j<cot;j++)
{
if (a[i][j]==max)
cout<<"\nVi Tri Max La Hang Thu "<< i+1<<" va Cot Thu "<<j+1<<" \n";
if (a[i][j]==min)
cout<<"\nVi Tri Min La Hang Thu "<< i+1<<" va Cot Thu "<<j+1<<" \n";
}
}

Chui
1 m khong trng
int demkhoangtrang(char *s)
{
int d=0;
while(strstr(s," ")!=NULL)
{
d++;
s=strstr(s," ")+1;
}
return d;
}
//lam theo ham de quy
// s=strstr(s," ");
//if(s==NULL)
//return 0;
//else return 1+demkhoangtrang(s+1);
2 Xa khong trng
void xoakhoangtrang(char *s)
{
char *c=strstr(s," ");
while(c!=NULL)
{
int t=strlen(s)-strlen(c);
for(int i=t;i<strlen(s);i++)
s[i]=s[i+1];
c=strstr(s," ");
}
if(s[0]==' ')
s=s+1; //xoa khang trang dau chuoi
int n=strlen(s);
if(s[n-1]==' ')
s[n-1]='\0';//xoa khoang trang cuoi chuoi
cout<<s; //for(int i=0;i<n;i++)
//cout<<s[i];
}
3 Ni chui
void noichuoi(char *a,char *b)
{
strcat(a,b);
puts(a);
}
4 Ch thng
void chuthuong(char *a)
{

for(int i=0;i<strlen(a);i++)
if(a[i]>=65 && a[i]<=90)
a[i]=a[i]+32;
puts(a);
}
5 Ch hoa
void chuhoa(char *a)
{
for(int i=0;i<strlen(a);i++)
a[i]=toupper(a[i]); //if(a[i]>=97 && a[i]<=122)
puts(a);
//a[i]=a[i]-32;
}
6 Ch hoa u t.
void chuhoadau(char *s)
{
s[0]=toupper(s[0]);
while(strstr(s," ")!=NULL)
{
s=strstr(s," ")+1;
s[0]=toupper(s[0]);
}
}
7. Ch hoa, ch thng xem k
void chuxenke(char *a)
{
int n=strlen(a);
for(int i=0;i<n;i++)
{
if(i%2==0)
if((int)a[i]>=97 && (int)a[i]<=122)
a[i]=char((int)a[i]-32);
if(i%2==1)
if((int)a[i]>=65 && (int)a[i]<=90)
a[i]=char((int)a[i]+32);
}
puts(a);
}
8 o chui
void daochuoi(char *s)
{
puts(strrev(s));
}

9 Tm kim 1 k t xem c trong chui khng


int vitri(char *a,char *b)
{
int kq,d=0;
if(strstr(a,b)!=NULL)
{
kq=strlen(a)-strlen(strstr(a,b));
for(int i=kq;i>=0;i--)
if(a[i]==' ')
d++;
return d+1;
}
else return -1;
}
10 m s ln xut hin ca t.
int diemtu(char *a,char *b)
{
int d=0;
while(strstr(a,b)!=NULL)
{
d++;
a=strstr(a,b)+1;
}
return d;
}
11 V tr xut hin chui 2 trong chui 1
int vitri(char *a,char *b)
{
int d=-1;
if(strstr(a,b)!=NULL)
d=strlen(a)-strlen(strstr(a,b));
return d;
}
12 Tm tn
void timten(char *a,char *b)
{
int n=strlen(a);
char *s;
for(int i=n-1;i>=0;i--)
if(a[i]==' ')
{
s=a+i+1;
break;
}
if(strcmpi(s,b)==0)// strcmp==stricmp
cout<<s;
else

cout<<"ban nhap sai ten\n";


}
13 o chui
void daochuoi(char *s)
{
char t[50],r[50];
int i,l;
for(i=strlen(s)-1;i>=0;i--)
if(s[i]==' ')
break;
strcpy(r,s+i+1);//tim dctu cuoi
s[i+1]='\0';//xau s sau khi bo tu cuoi,co khoang trang o cuoi xau
l=strlen(s)-strlen(strstr(s," ")); //tim vi tri khoang trang dau tien trong chuoi
strcpy(t,s+l+1);//l la xau chau cac tu chinh giua
s[l]='\0'; //hien tai la xau chua tu dautien
strcat(strcat(strcat(r," "),t),s);//noi cac xau lai voi nhau*/
puts(r);
}
14 Ct h tn
void cathoten(char *s)
{
char *t;
int i;
for(i=strlen(s)-1;i>=0;i--)
if(s[i]==' '&&s[i+1]!=' ')
break;
t=s+i+1;
s[i]='\0';
cout<<"ho lot: ";
puts(s);
cout<<"ho ten: ";
puts(t);
}
15 Tm t
void timtu(char *a,char *b)
{
char *t=strstr(a,b);//vi tri xuat hien b dau tien trong chuoi a
puts(t);
}
16 Kim tra i xng
bool ktdoixung(char *s)
{
char c[255];
strcpy(c,s);

if(strcmpi(s,strrev(c))==0)
return true;
else return false;
}
17 Tch s
void tachso(char *s)
{
int a[100],j=0;
for(int i=0;i<strlen(s);i++)
if(s[i]>='0' && s[i]<='9')
{
a[j]=s[i];//ki tu kieu char chuyen thanh kieu int(vd:1=>49)
j++;
for(int t=i;t<strlen(s);t++)
s[t]=s[t+1];
i--;
}
puts(s);
for(int i=0;i<j;i++)
cout<<char(a[i])<<" ";
}
18 Xa k t
void xoakitu(char *a,char b)
{
int n=strlen(a);
for(int i=0;i<n;i++)
if(a[i]==b)
{
for(int j=i;j<n;j++)
a[j]=a[j+1];
i--;
n--;
}
cout<<a;
}
19 ch hoa
void chuhoa(char *s)
{
s[0]=toupper(s[0]);
for(int i=1;i<strlen(s);i++) //chua tim dc cach nao toi uu hon
if(s[i]==' ')
{
s[i+1]=toupper(s[i+1]);
i++;
}
else

{
if(s[i]>='A' && s[i]<='Z')
s[i]=s[i]+32;
}
puts(s);
}
20 o th t
void daothutu(char *s)
{
char c[255];
c[0]='\0';//ham strcat noi chuoi tai vi tri NULL
for(int i=strlen(s)-1;i>=0;i--)
if(s[i]==' ')
{
strcat(strcat(c,s+i+1)," ");
s[i]='\0';
}
strcat(c,s);
puts(c);
}
21 Xa k t ti v tr n.
void xoa(char *s,int vt,int n)
{
strcpy(s+vt,s+vt+n);
puts(s);
}
22 chn k t.
void chen(char *s,char *d,int vt)
{
char c[255];
strcpy(c,s+vt);
strcpy(s+vt,d);
strcat(s,c);
puts(s);
}
23 Xa t.
void xoatu(char *s,char *c)
{
while(strstr(s,c)!=NULL)
{
int t=strlen(s)-strlen(strstr(s,c)),d=strlen(c);
if((s[t-1]==' '&& s[t+d]==' ')||(s[t-1]==' '&& t+d==strlen(s)))//truong
hop tu giua va tu cuoi

strcpy(s+t-1,s+t+d);// s+t-1 la khoang trang


if(s[t+d]==' '&& t==0)//truong hop xoa tu dau tien
strcpy(s,s+t+d+1);
}
puts(s);
}
24 K t xut hin nhiu nht
void kituxuathiennhieunhat(char *s)
{
int a[100],n=0;
for(int i=0;i<(int)strlen(s);i++)
{
int d=1;
for(int j=i+1;j<(int)strlen(s);j++)
if(s[i]==s[j])
{
d++;
for(int k=j;k<(int)strlen(s);k++)
s[k]=s[k+1];
j--;
}
a[n++]=d;
}
int max=a[0];
for(int i=1;i<(int)strlen(s);i++)
if(a[i]>max)
max=a[i];
for(int i=1;i<(int)strlen(s);i++)
if(a[i]==max)
cout<<"ki tu "<<s[i]<<" xuat hien nhieu nhat la "<<a[i]<<" lan\n";
}
25 m k t
void demkitu(char *s)
{
int a[100],n=0;
for(int i=0;i<(int)strlen(s);i++)
{
int d=1;
for(int j=i+1;j<(int)strlen(s);j++)
if(s[i]==s[j])
{
d++;
for(int k=j;k<(int)strlen(s);k++)
s[k]=s[k+1];
j--;
}
a[n++]=d;

}
for(int i=0;i<(int)strlen(s);i++)
cout<<"ki tu "<<s[i]<<" xuat hien "<<a[i]<<" lan\n";
}
Bi chui
#include<iostream.h>
#include<string.h>
#include<iomanip.h>
struct hocsinh
{
char ten[20];
char masinhvien[10];
char namsinh[10];
int toan,ly,hoa;
float tb;
}
hs['n'];
void nhap(hocsinh hs['n'], int &n)
{
cout<<"nhap vao so hs :";
cin>>n;
cin.get();
for(int i=1;i<=n;i++)
{
cout<<"Hoc sinh thu "<<i<<" :\n";
cout<<"nhap ho va teen :";
cin.getline(hs[i].ten,20);
cout<<"Nhap ma so sinh vien :";
cin.getline(hs[i].masinhvien,10);
cout<<"Nhap nam sinh :";
cin.getline(hs[i].namsinh,10);
cout<<"Nhap diem toan:";
cin>>hs[i].toan;
cin.get();
cout<<"Nhap diem Ly :";
cin>>hs[i].ly;
cin.get();
cout<<"Nhap diem hoa :";
cin>>hs[i].hoa;
cin.get();
}
}
void diemtb( hocsinh hs['n'], int n)
{
cout<<"Diem trung binh cua cac hs la :\n";
for(int i =1; i<=n;i++)
{

hs[i].tb = float(hs[i].toan+hs[i].hoa+hs[i].ly)/3;
cout<<"hoc sinh "<<hs[i].ten<<" co diem trung binh la:"<<hs[i].tb<<endl;
}
}
void xuat(hocsinh hs['n'], int n)
{
for(int i=1;i<=n;i++)
cout<<"\nHo Va Ten: "<<hs[i].ten<<"\nDiem Toan: "<<hs[i].toan<<"\nDiem ly:
"<<hs[i].ly<<"\nDiem hoa: "<<hs[i].hoa<<"\nDiem TB: "<<hs[i].tb;
}
void main()
{
int n;
nhap(hs,n);
diemtb(hs,n);
xuat(hs,n);
}

Con tr
Bi 1
#include<iostream.h>
#include<conio.h>
void nhap( int *a, int n)
{
for(int i=0;i<n;i++)
{
cout<<"nhap vao phan tu thu "<<i<<":";
cin>>a[i];
}
}
void xuat (int *a, int n)
{
cout<<"mang da nhap la: ";
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
}
void tong(int *a, int n)
{
int dem1=0;
int dem2=0;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
dem1=dem1+a[i];
}
else
dem2=dem2+a[i];
}
cout<<"\n tong chan la:"<<dem1;
cout<<"\n tong le la:"<<dem2<<"\n";
}
int ktsnt(int x)
{
if(x<=1)
return 0;
for(int i=2;i<x;i++)
{
if(x%i==0)
return 0;
}
return 1;
}
void tongngto(int *a, int n)

{
int tong=0;
for(int i=0;i<n;i++)
{
if(ktsnt(a[i])==1)
{
tong=tong+a[i];
}
}
cout<<"\n tong so ngto la: "<<tong<<"\n";
}
void sxchan( int *a, int n)
{
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
{
for(int j=i+1;j<=n;j++)
{
if(a[j]%2==0)
{
if(a[j]<a[j])
{
int t =a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
}
cout<< a[i]<<"\t";
}
void main()
{
int *a;
int n;
a= new int[n];
cout<<"nhap n: ";
cin>>n;
nhap(a,n);
xuat(a,n);
tong(a,n);
tongngto(a,n);
sxchan(a,n);
getch();

Bi 2.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void nhap(int **A, int row, int colum)
{
for(int i=0;i<row;i++)
{
A[i]=new int [colum];
for(int j=0;j<colum;j++)
{
cout<<"Nhap ["<<i<<","<<j<<"]= ";
cin>>A[i][j];
}
}
}
void xuat(int **A, int row, int colum)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<colum;j++)
cout<<A[i][j]<<"\t";
cout<<"\n"<<"\n";
}
}
void congmang(int **A, int **B, int row, int colum)
{
for(int i=0;i<row;i++)
for(int j=0;j<colum;j++)
{
A[i][j]= A[i][j]+B[i][j];
}
xuat(A,row,colum);
}
void doicho(int &a,int &b)
{
int tam;
tam=a;
a=b;
b=tam;
}
void xepdong(int **A, int row, int colum)
{
for(int i=0;i<row;i++)
for(int j=0;j<colum; j++)
{
for(int k=j+1;k<colum;k++)
if(A[i][j]<A[i][k])

{
doicho(A[i][j],A[i][k]);
}
}
xuat(A,row,colum);
}
void tongbien(int **A, int row, int colum)
{
cout<<"Tong bien se l:\n";
int T=0;
for(int i=0;i<row;i++)
for(int j=0; j<colum; j++)
{
if(i==0||i==row-1||j==0||j==colum-1)
{
T+=A[i][j];
}
}
cout<<T;
}
int sohoanthien(int &n)
{
int tong=0;
for(int i=1;i<n; i++)
if(n%i==0)
tong=tong+i;
if( tong==n)
return 1;
return 0;
}
void tongsohoanthien(int **A, int row, int colum)
{
int HT=0;
for( int i=0;i<row; i++)
for( int j=0;j<colum;j++)
{
if(sohoanthien(A[i][j])==1)
{
HT += A[i][j];
}
}
cout<<HT;
}
int songto(int n)
{
if(n<=1)
return 0;
for(int i=2;i<n;i++)
if(n%i==0)
return 0;

return 1;
}
void tongsongto(int **A, int row, int colum)
{
int tong=0;
for(int i=0;i<row;i++)
for( int j=0;j<colum; j++)
{
if (songto(A[i][j])==1)
{
tong+=A[i][j];
}
}
cout<<tong;
}
int amdautien(int **A, int row, int colum)
{
for(int i=0;i<row;i++)
{
for(int j= 0;j<colum;j++)
if(A[i][j]<0)
{
return A[i][j];
}
}
return 0;
}
void amlonnhat(int **A, int row,int colum)
{
cout<<"\ngia tri am max la :";
int ammax= amdautien(A,1,colum);
for(int i=0;i<row;i++)
for(int j=0;j<colum;j++)
if(A[i][j]<0)
{
if(A[i][j]>ammax)
{
ammax=A[i][j];
}
}
cout<<ammax;
}
void main()
{
int **A, **B, row, colum;

A= new int *[row];


B= new int *[row];
cout<<"nhap so dong :";
cin>>row;
cout<<"nhap so cot :";
cin>>colum;
cout<<"KhOi TaO MAng A:\n";
nhap(A,row,colum);
cout<<"KhOi TaO MAng B:\n";
nhap(B,row,colum);
cout<<"mang A la :\n";
xuat(A,row,colum);
cout<<"mang B la :\n";
xuat(B,row,colum);
cout<<"Tong 2 mang la:\n";
congmang(A,B,row,colum);
cout<<"sap sep theo dong la:\n";
xepdong(A,row,colum);
tongbien(A,row,colum);
cout<<"\n Tong so Hoan Thien lA:\n";
tongsohoanthien(A,row,colum);
cout<<"\n tong so nguyen to la :";
tongsongto(A, row,colum);
amlonnhat(A,row,colum);
getch();
}

You might also like