Phản hồi: Tất cả tài liệu về Toán Rời Rạc Post ở đây
- Bài 1 : Tính chỉnh hợp
Code:
#include<iostream.h>
#include<conio.h>
int GiaiThua(int n)
{
int Gt=1;
for (int i=1;i<=n;i++)
{ Gt*=i; }
return Gt;
}
int ChinhHop(int k ,int n)
{
int Ch;
return Ch=((GiaiThua(n))/(GiaiThua(n-k)));
}
void main()
{ int n;
int k;
cout<<"nhap k=";
cin>>k;
cout<<"nhap n=";
cin>>n;
cout<<"Chinh hop chap"<<" " << k<<" " << "cua"<<" " << n <<" la:"<< ChinhHop(k,n)<< " " ;
}
- Bài 2 : Liệt kê dãy nhị phân
Code:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#define MAX 100
typedef int vector[MAX];
vector b;
int n,count;
void init(int &count, int &n)
{
cout<<"Nhap so cua chuoi bit:"; cin>>n;
count=0;
}
void result(vector b)
{
int i;
count+=1; cout<<count<<": ";
for(i=1;i<=n;i++)
cout<<b[i]<<" ";
cout<<"\n";
}
void Try(int i, vector b)
{
int j;
for(j=0;j<=1;j++)
{
b[i]=j;
if(i==n) result(b);
else Try(i+1, b);
}
}
void main()
{
char ans;
do
{ init(count, n);
Try(1, b);
cout << " Ban co muon tiep tuc ko (c/k)?";
cin >> ans ;
}while ( ans=='c' || ans=='C' );
}
- Bài 3 : Liệt kê hoán vị
Code:
#include<iostream.h>
#define MAX 20
typedef int vector[MAX];
int n,count;
vector p,b; //b la bien trang thai boolean
void init(vector p)
{
int i;
cout<<"Ban muon hoan vi bao nhieu phan tu ?n="; cin>>n;
for(i=1;i<=n;i++) b[i]=1;
count=0;
}
void result(vector p)
{
int i;
count+=1; cout<<count<<": ";
for(i=1;i<=n;i++)
cout<<p[i]<<" ";
cout<<"\n";
}
void Permutation(int i, vector &p)
{
int j;
for(j=1;j<=n;j++)
if(b[j]) //Chap nhan j
{
p[i]=j;
b[j]=0; //ghi nhan trang thai moi
if(i==n) result(p);
else Permutation(i+1, p);
b[j]=1; //tra lai trang thai cu
}
}
void main()
{
char ans;
do
{
init(p);
Permutation(1,p);
cout << "Ban co muon tiep tuc hay ko (C/K)?";
cin >> ans;
} while (ans=='c'||ans=='C');
}
- Bài 4 : Liệt kê tổ hợp chập K của N phần tử
Code:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#define max 100
typedef int vector[max];
void init(int &k, int &n, int &count, vector c)
{
cout<<"enter n="; cin>>n;
cout<<"enter k="; cin>>k;
cout<<"combinations of consisting "<<k<<" of "<<n<<"\n";
c[0]=0;
count=0;
}
void result(int k, int &count, vector c)
{
int i;
count+=1; cout<<count<<": ";
for(i=1;i<=k;i++)
cout<<c[i]<<" ";
cout<<"\n";
}
void combination(int i, int k, int n, int &count, vector c)
{
int j;
for(j=c[i-1]+1;j<=n-k+i;j++)
{
c[i]=j; //xac dinh c[i] theo j
if(i==k) result ( k, count , c );
else combination(i+1, k, n, count, c);
}
}
void main()
{
int k,n,count;
vector c;
init(k, n, count, c);
combination(1, k, n, count, c);
}
- Bài 5 : Liệt kê chỉnh hợp chập K của N phần tử
Code:
#include<iostream.h>
#define MAX 20
typedef int vector[MAX];
int n,count,k;
vector p,b; //b la bien trang thai boolean
void init(vector p)
{
int i;
cout<<"n="; cin>>n;
cout<<"k=";cin>>k;
for(i=1;i<=n;i++) b[i]=1;
count=0;
}
void result(vector p)
{
int i;
count+=1; cout<<count<<": ";
for(i=1;i<=k;i++)
cout<<p[i]<<" ";
cout<<"\n";
}
void Permutation(int i, vector &p)
{
int j;
for(j=1;j<=n;j++)
if(b[j]) //Chap nhan j
{
p[i]=j;
b[j]=0; //ghi nhan trang thai moi
if(i==k) result (p);
else Permutation(i+1, p);
b[j]=1; //tra lai trang thai cu
}
}
void main()
{
init(p);
Permutation(1,p);
}
- Bài 6 : Liệt kê dãy nhị phân không có hai số 0 liên tiếp
Code:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#define max 40
typedef int vector[max];
int n,count;
vector b;
void init(vector b, int &count, int &n)
{
cout<<"n="; cin>>n;
count=0;
b[0]=1; // de thoa dieu kien trong ham try(i)
}
void result(vector b)
{
int i;
count+=1; cout<<count<<": ";
for(i=1;i<=n;i++)
cout<<b[i]<<" ";
cout<<"\n";
}
void binary(int i, vector b)
{
int j;
for(j=0;j<=1;j++)
if(b[i-1]+j)
{
b[i]=j;
if(i==n) result(b);
else binary(i+1, b);
}
}
void main()
{
init(b, count, n);
binary(1, b);
}
- Bài 7 : Liệt kê dãy nhị phân
Code:
//chuong trinh liet ke day nhi phan
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#define MAX 100
typedef int vector[MAX];
vector b;
int n,count;
void init(int &count, int &n)
{
cout<<"given number of bits of strings n="; cin>>n;
count=0;
}
void result(vector b)
{
int i;
count+=1; cout<<count<<": ";
for(i=1;i<=n;i++)
cout<<b[i]<<" ";
cout<<"\n";
}
void Binary(int i, vector b)
{
int j;
for(j=0;j<=1;j++)
{
b[i]=j;
if(i==n) result(b);
else Binary(i+1, b);
}
}
void main()
{
init(count, n);
Binary(1, b);
}
- Bài 8 : Tìm phần tử lớn nhất của dãy
Code:
# include <iostream.h>
# include <assert.h>
# define MAX 50
int Max ( int x , int y )
{
if ( x >= y )
return x ;
else
return y ;
}
int MaxList ( int a[] , int d , int c )
{
int kq ;
if ( d < c )
{
if ( d == c )
kq = a[d] ;
else if ( c == d+1 )
kq = Max ( a[d] , a[c] ) ;
else
{
int m = (c+d)/2 ;
int x = MaxList ( a , d , m ) ;
int y = MaxList ( a , m+1 , c ) ;
kq = Max ( x , y ) ;
}
}
return kq ;
}
int main ()
{
int n , a[MAX] ;
cout <<" Nhap so phan tu cua day : " ; cin >> n ;
cout <<" Nhap vao day so " ;
for ( int i = 1 ; i <= n ; i++ )
cin >> a[i] ;
cout <<" Phan tu lon nhat cua day la : "<< MaxList ( a , 1 , n ) << endl ;
return 0 ;
}
Download:http://lazycatnct.googlepages.com/OnTapToanTin.doc
Nguyễn Thu Hương
Smod Nhịp sống sinh viên
YH: nguyenthithuhuong_21071991 Mail: thuhuong217@gmail.com
|
|