当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 顺序求出c(n,r)的排列组合

ASP.NET
.NET中如何生成静态页
ASP.NET定制简单的错误处理页面
在客户端验证密码强度[2],兼容FireFox和IE
如何以及为何创建Search .NET版
ASP.NET十分有用的页面间传值方法
使用ASP.NET AJAX框架扩展HTML Map控件
AJAX使用技巧:如何处理书签和翻页按扭
.NET环境下几种不同的邮件发送解决方案
.NET vs J2EE——面对SOA的荒谬与误解
ASP.NET学习篇(1)——开篇
ASP.NET学习篇(2)——安装与配置
ASP.NET学习篇(3)——几个简单的ASP.ENT的例子
ASP.NET学习篇(4)——服务器端的控件
项目调试时出现用到的一个组件“访问被拒绝”的解决方法
ASP.NET中“找不到指定模块”的解决办法
采用Native 引导方式的.Net加密保护
“您无权查看该网页”的原因和解决方法
ASP.NET中Datagrid常见错误
编写ASP.NET应用程序的技巧
Scott Mitchell ASP.NET 2数据控件嵌套

ASP.NET 中的 顺序求出c(n,r)的排列组合


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 59 ::
收藏到网摘: n/a

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