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

2016-01-29 13:30 27 1 收藏

顺序求出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();
   }
  }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com)

  //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;
  }
 }
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com)

来源:https://www.tulaoshi.com/n/20160129/1490406.html

延伸阅读
排列 代码如下: #include stdio.h // 主要是找到当前要排的 和后面要排数的关系 int swap(int m,int n) {  if(n==1)   return m-n+1;  return  m*swap(m-1,n-1); } int main() {  int m=5,n=4;  printf("%d",swap(5,4)); } 组合 计算3个A,2个B可以组成多少种排列的问题 思路一: 代码如下:...
标签: 电脑入门
Mac OS系统工具栏的图标顺序,是默认排好的,但是有一些用户并不喜欢这种排序方式,因为这样会影响他们的操作习惯。那么Mac系统怎样改变工具栏图标顺序呢?现在就让图老师小编给你介绍一个小方法吧。 在Mac OS X系统里很多程序的工具栏我们都可以快速的随意改变,例如,Finder或者预览等。改变的方法很简单,你按住键盘上的Command不动,然后...
最高 ! ++ -- * / % + - = is== !=&^|&&(短路与)|| (短路或)?:最低
标签: 智能手机
镜面与钻石的融合 现在是个看脸的时代,就连手机也不例外,所以手机厂商纷纷在外观上下足了功夫,这次OPPO选择了将镜面和钻石这两个元素进行结合,得到的作品就是今天我们手上的R1C,那么问题来了,这款特别的手机能在各位的心目之中打几分呢? OPPO传统的白盒子,这回我们很幸运地同时拿到了蓝色和白色的R1C。 盒子背部是基本说明。...

经验教程

738

收藏

11
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部