برج های هانوی به صورت بازگشتی
کد:
#include<iostream.h>
#include<conio.h>
void GHOST(int n,char a,char b,char c);
void main()
{
clrscr();
int m;
cout<<"Enter n:";
cin>> m;
GHOST(m,'A','B','C');
getch();
}
void GHOST(int n,char a,char b, char c)
{
if(n==1)
cout<<"move disk 1 form "<<a<<" to "<<c<<"\n";
else
{
GHOST(n-1,a,c,b);
cout<<"move disk "<<n<<" form "<<a<<" to "<<c<<"\n";
GHOST(n-1,b,a,c);
}
}
پیدا کردن یک عدد در یک آرایه به صورت تقسیم غلبه
کد:
#include<iostream.h>
#include<conio.h>
int bin(int x,int a[],int low,int high)
{
int mid;
if(low>=high) return -1;
mid=(low+high)/2;
if(x==a[mid]) return mid;
if(x<a[mid]) return bin(x,a,low,mid-1);
if(x>a[mid]) return bin(x,a,mid+1,high);
}
void main()
{
clrscr();
int z;
int p[4];
p[0]=1;
p[1]=2;
p[2]=3;
p[3]=4;
cin>>z;
cout<< bin(z,p,0,4);
getch();
}
N به توان X به صورت بازگشتی
کد:
#include<conio.h>
#include<iostream.h>
long int xton(float x,int n)
{
if (n==1)
return x;
else
if (n%2==0) return xton(x,n/2)*xton(x,n/2);
else
return xton(x,n/2)*xton(x,n/2)*x;
}
void main()
{
clrscr();
int a,b;
cin>>a;
cin>>b;
cout<<xton(a,b);
getch();
}
برنامه کوله پشتی
کد:
#include<iostream.h>
#include<conio.h>
float p[5]={40,9,36,24,25}; //arzesh
float w[5]={20,18,12,16,10}; //vazn
float k[5];
void sort()
{
float m,n;
for(int i=0;i<5;i++)
{
m=p[i]/w[i];
for(int j=4;j>=i;j--)
{
n=p[j]/w[j];
if(m<n)
{
int tp=p[i];
p[i]=p[j];
p[j]=tp;
int tw=w[i];
w[i]=w[j];
w[j]=tw;
m=p[j]/w[j];
}
}
}
}
void kole(int m)
{
int c=m;
int x=0,j=0;
for(int i=0;i<c;i++)
{
if(w[j]<c)
{
k[j]=1;
c=c-w[j++];
}
else
{
k[j]=c/w[j++];
c=c-c;
}
}
}
void main()
{
clrscr();
sort();
for(int i=0;i<5;i++)
{
cout<<p[i]<<" ";
}
cout<<"\n";
for(int j=0;j<5;j++)cout<<w[j]<<" ";
cout<<"\n";
kole(50);
for(i=0;i<5;i++)
cout<<k[i]<<" ";
getch();
}
پیدا کردن بزرگ ترین و کوچک ترین عدد در آرایه
کد:
#include<iostream.h>
#include<conio.h>
int A[100], low, high, Min=0, Max=0;
void MinMax(int A[], int low, int high, int &Min, int &Max)
{
int MinL, MaxL, MinR, MaxR;
if(low==high)
{
Min=Max=A[low];
return;
}
if(low==high-1)
{
if(A[low]<A[high])
{
Min=A[low];
Max=A[high];
return;
}
else
{
Min=A[high];
Max=A[low];
return;
}
}
int mid=(low+high)/2;
MinMax(A, low, mid, MinL, MaxL);
MinMax(A, mid+1, high, MinR, MaxR);
if(MinL<MinR)
Min=MinL;
else
Min=MinR;
if(MaxL>MaxR)
Max=MaxL;
else
Max=MaxR;
}
void main()
{
clrscr();
int i, n;
cout<<"Please Enter Array lengh: ";
cin>>n;
cout<<"\nPlease Input Array Elements: \n";
for(i=0; i<n; i++)
{
cout<<" A["<<i+1<<"]:";
cin>>A[i];
}
MinMax(A, 0, n-1, Min, Max);
cout<<"The Minimum Value is: "<<Min<<"\n"<<"The Maximum Value is: "<<Max;
getch();
}
الگوریتم پریم
کد:
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
struct krus{
int v1;
int v2;
int weight;
};
int perim (int set[],struct krus edge[],int n,int m);
void sort(struct krus ed[],int m);
void main()
{
clrscr();
int n,m;
cout<<"Input Num Vertex : ";
cin>>n;
int set[10];
for (int i=0;i<n;i++)
set[i]=i;
cout<<"Input Num Yal : ";
cin>>m;
struct krus edge[20];
for (i=0;i<m;i++)
{
cout<<" Num V1 : "; cin>>edge[i].v1;
cout<<" Num V2 : "; cin>>edge[i].v2;
cout<<" Weight : "; cin>>edge[i].weight;
gotoxy(wherex()+30,wherey()-2);
cout<<"("<<edge[i].v1<<","<<edge[i].v2<<") => W :"<<edge[i].weight<<"\n";
gotoxy(1,wherey()+2);
}
cout<<"\nWeight Is : "<<perim(set,edge,n,m);
getch();
}
//***********************************************
int perim(int set[],struct krus edge[],int n,int m)
{
int fe=0;
int p=0;
struct krus e;
while (fe<n-1)
{
//********************************
int y=0;
e.weight=0;
for (int i=0;i<m;i++)
if ((set[edge[i].v1]==0 && set[edge[i].v2]!=0) || (set[edge[i].v2]==0 && set[edge[i].v1]!=0))
{
if(y==0)
{
e=edge[i];
y++;
}
else
if (e.weight>edge[i].weight)
e=edge[i];
}
//**********************************
if (y!=0)
{
p+=e.weight;
cout<<"("<<e.v1<<","<<e.v2<<") => W :"<<e.weight<<"\t";
set[e.v1]=0;
set[e.v2]=0;
fe++;
}
else
break;
}
return p;
}
الگوریتم پریم
کد:
#include<iostream.h>
#include<conio.h>
float gprim (float cost[][n], int t[][2],int n)
{
float mincost,mindist[];
int near[],b[];
for(i=0;i<n;i++)
b[i]=0;
b[0]=1;
mincost=0;
near = -1;
mindist[0] = MAXFLOAT;
for(i=1;i<n;i++)
{
near[i]=0;
mindist[i]=cost[i][0];
}
for(i=0;i<n-1;i++)
{
j=minedge(b,mindist);
t[i][0]=j;
t[i][1]=near[j];
b[j]=1;
mincost = mincost + cost[i][near[j]];
mindist[j] = MAXFLOAT;
near[j] = -1;
for(int l=0;l<n;l++)
if(b[l] == 0 && cost[l][near[l]] > cost[l][j])
{
near[l]=j;
mindist[l]= cost[l][j];
}
}
return mincost;
}
void main() {
clrscr();
getch();
}
الگوریتم Merge Sort
کد:
#include<conio.h>
#include<iostream.h>
int a[4],b[4];
void merge(int low, int mid ,int high);
void mergesort(int low,int high)
{
int mid;
if(low < high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low, int mid ,int high)
{
int h=low;
int j=mid+1;
int i=low;
int k;
while (h<=mid && j<= high)
{
if(a[h]<a[j])
b[i++]=a[h++];
else
b[i++]=a[j++];
}
if (h > mid)
for(k=j;k<=high;k++,i++)
b[i]=a[k];
if (j > high)
for(k=h;k<=mid;k++,i++)
b[i]=a[k];
for(k=low;k<=high;k++)
a[k]=b[k];
}
void main()
{
clrscr();
a[0]=3;
a[1]=1;
a[2]=2;
a[3]=4;
mergesort(0,3);
for(int i=0;i<=3;i++)
cout<<"\t"<<a[i];
getch();
}
موفق باشید.