1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include<iostream.h>
#include<math.h>
#include<eku\ekulib.h>
bool ismore(int* a,int n);//check if array can be further incremented
bool next(int* a,int n);//put the next number in the array storing number of
//substituents at each C
void init(int* a,int n,int r);//initialize an array storing number of substituents at each C
//n - no. of C atoms in principal chain r - no. of C atoms as substituents
int iso(int n,int r);//gives the number of isomers of a carbon compound with
//n C atoms in principal chain and r C atoms as substituents
int isoall(int n,int r);//gives all isomers with n C atoms and maximum length of
//principal chain as r
inline int maxalk(int n)
{return int(pow(3,double(n))-1)/2;}
bool ismore(int* a,int n)
{
int i,r=0,t;
for(i=0;i<n;i++)r+=a[i];
if(r==0)return false;
for(i=0;i<n;i++)
{
t=maxalk(i);
if(a[i]>=t || r==a[i])r-=a[i];
else return true;
}
return false;
}
bool next(int* a,int n)
{
int i,r=0;
for(i=0;i<n;i++)r+=a[i];
if(n==3 && ismore(a,3)){a[1]++;a[2]--;return true;}
else if(!ismore(a,n))return false;
else if(ismore(a,n-1)){next(a,n-1);return true;}
else {a[n-2]++;a[n-1]--;return true;}
}
void init(int* a,int n,int r)
{
int p=r,t,i;
for(i=n-1;i>=0;i--)
{
if(p==0)a[i]=0;
else
{
t=maxalk(i);
if(p<=t){a[i]=p;p=0;}
else {a[i]=t;p-=i;}
}
}
}
int iso(int n,int r)
{
int *a,i,j,count=0,p,q;
if(r>maxalk(n)-n || r<0)return 0;
if(n==1 || n==2 || r==0)return 1;
a=new int[n];
init(a,n,r);
do
{
for(i=1;i<n;i++)for(j=0;j<=a[i]/2;j++)
{
p=isoall(j,i);
q=isoall(a[i]-j,i);
if(p==0 || q==0)count+=p+q;
else count+=p*q;
}
}
while(next(a,n));
delete[] a;
return count;
}
int isoall(int n,int r)
{
if(n<r)r=n;
if(r<=0 || n==0)return 0;
else if(n==1 || n==2)return 1;
int i,s=0;
for(i=2;i<=r;i++)if(n-i<=maxalk(i)-i)s+=iso(i,n-i);
return s;
}
void main()
{
int n;
A: cout<<"Enter n ";cin>>n;
cout<<"Isomers of alkyl groups with "<<n<<" carbons = "<<isoall(n,n)<<endl;
if(cont())goto A;
}
|