顺序求出c(n,r)的排列组合,顺序求出c(n,r)的排列组合
【 tulaoshi.com - ASP.NET 】
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;
}
}
}
来源:http://www.tulaoshi.com/n/20160129/1490406.html
看过《顺序求出c(n,r)的排列组合》的人还看了以下文章 更多>>