原:求组合数问题!

我的Java实现:

package com.herosoft.algorithm;

import java.io.*;

class Combination {
private int count;

private int[] s;

public Combination(int n, int r) {
if (n < r) {
int temp;
temp = n;
n = r;
r = temp;
}
s = new int[n];
count = count(n, r);
System.out.println("there are total:count: " + count);
for (int i = 0; i < r; i++) {
s[i] = i + 1;
System.out.printf("%2d", s[i]);
}
System.out.println();

permutation(n, r);
}

void permutation(int n, int r) {

for (int i = 1; i < count; i++) {
int m = r - 1, max_value = n;

// notice: good logic
while (s[m] == max_value) {
m = m - 1;
max_value = max_value - 1;
}
// if (s[m] + 1 == s[0])
// s[m] = s[m] + 1;
s[m] = s[m] + 1;

for (int j = m + 1; j < r; j++) {
s[j] = s[j - 1] + 1;
}
for (int k = 0; k < r; k++) {
System.out.printf("%2d", s[k]);
}
System.out.println();
}
}

// compute value of c(n,r)
static int count(int n, int r) {
int sn = 1, sr = 1, s_r = 1;
for (int i = 1; i <= n; i++) {
sn *= i;
}
for (int j = 1; j <= r; j++) {
sr *= j;
}
for (int k = 1; k <= n - r; k++) {
s_r *= k;
}

sr *= s_r;
return sn / sr;
// combination logic: only return sn/s_r, not to need sr
// return sn/s_r;
}

public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("please input n :");
int n = Integer.parseInt(in.readLine());
System.out.println("please input r :");
int r = Integer.parseInt(in.readLine());
new Combination(n, r);
}
}

以下摘自求组合数问题!http://topic.youkuaiyun.com/t/20050808/09/4194872.html

[@more@]1.递归...
【问题】 组合问题
问题描述:找出从自然数1、2、……、n中任取r个数的所有组合。例如n=5,r=3的所有组合为: (1)5、4、3 (2)5、4、2 (3)5、4、1
(4)5、3、2 (5)5、3、1 (6)5、2、1
(7)4、3、2 (8)4、3、1 (9)4、2、1
(10)3、2、1
分析所列的10个组合,可以采用这样的递归思想来考虑求组合函数的算法。设函数为void comb(int m,int k)为找出从自然数1、2、……、m中任取k个数的所有组合。当组合的第一个数字选定时,其后的数字是从余下的m-1个数中取k-1数的组合。这就将求m个数中取k个数的组合问题转化成求m-1个数中取k-1个数的组合问题。设函数引入工作数组a[ ]存放求出的组合的数字,约定函数将确定的k个数字组合的第一个数字放在a[k]中,当一个组合求出后,才将a[ ]中的一个组合输出。第一个数可以是m、m-1、……、k,函数将确定组合的第一个数字放入数组后,有两种可能的选择,因还未去顶组合的其余元素,继续递归去确定;或因已确定了组合的全部元素,输出这个组合。细节见以下程序中的函数comb。
【程序】
# include
# define MAXN 100
int a[MAXN];
void comb(int m,int k)
{ int i,j;
for (i=m;i>=k;i--)
{ a[k]=i;
if (k>1)
comb(i-1,k-1);
else
{ for (j=a[0];j>0;j--)
printf(“%4d”,a[j]);
printf(“n”);
}
}
}

void main()
{ a[0]=3;
comb(5,3);
}
2.穷举搜索法(0和1)
#include
#include
using namespace std;
const int N=5;
int b[N];
void prc_num(int x)
{
int i;
for(i=1;i<=N;i++)
{
if((1<else b[i-1]=0;
}
}
int main()
{
int arr[]={1,2,3,4,5};
int sum=3;
for(int i=0;i{
prc_num(i);
int temp=0;
for(int j=0;j{
if(b[j]==1)temp+=1;
}
if(temp==sum)
{
for(int t=0;t{
if(b[t]==1)cout<}
cout<}
}
cout<system("PAUSE");
return 0;
}

#include

using namespace std;
int m,n,*p;
void main()
{
cin>>m>>n;
p=new int[n+1];

for(int i=0;i{
p[i]=i+1;
}
p[n]=m+1;

while(p[0]!=m-n+1)
{
for(i=0;i{
cout<

}
cout<int k=n-1;
while(p[k]==m-n+k+1)
{
k--;
}

p[k]++;
for(int a=k+1;a{
p[a]=p[a-1]+1;
}
}

for(i=0;i{
cout<}

cout<}

http://topic.youkuaiyun.com/t/20050808/09/4194872.html

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220284/viewspace-1028102/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/220284/viewspace-1028102/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值