动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   .Net → 阅读文章

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

作者:无从考证 来源:中国软件 
阅读 2503 人次 , 2006-2-15 14:26:00 

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

  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:我在使用C#中Treeview与解析XML遇到的问题
· 下一篇:asp.net生成静态页
· 使用 ASP+ 列表绑定控件(下)
· SQL命令中DateTime格式参考
· Asp.net编写的PING工具
· 通过网络域名得到这台主机的IP地址
· Jsp + JavaBean循序渐进教程


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077