顺序求出c(n,r)的排列组合

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

顺序求出c(n,r)的排列组合

顺序求出c(n,r)的排列组合 using System;

namespace combinaton

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//

Console.WriteLine('please input n :');

long n=long.Parse(Console.ReadLine());

Console.WriteLine('please input r :');

long r=long.Parse(Console.ReadLine());

combinaton(n,r);

Console.ReadLine();

}

static void combinaton(long n,long r)

{

if(n<r)

{

long temp;

temp=n;

n=r;

r=temp;

}

long[] s=new long[n];

long count = combi(n,r);

Console.WriteLine('there are total:{0}count:',count);

for(long i=0;i<r;i++)

{

s[i]=i+1;

Console.Write('{0}',s[i]);

}

Console.WriteLine();

for(long i=1;i<count;i++)

{

long m=r-1,max_value=n;

while(s[m]==max_value)

{

m=m-1;

max_value=max_value-1;

}

s[m]=s[m]+1;

for(long j=m+1;j<r;j++)

{

s[j]=s[j-1]+1;

}

for(long k=0;k<r;k++)

{

Console.Write('{0}',s[k]);

}

Console.WriteLine();

}

}

//compute value of c(n,r)

static long combi(long n,long r)

{

long sn=1,sr=1,s_r=1;

for(long i=1;i<=n;i++)

{

sn*=i;

}

for(long j=1;j<=r;j++)

{

sr*=j;

}

for(long k=1;k<=n-r;k++)

{

s_r*=k;

}

sr*=s_r;

return sn/sr;

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航