C语言1100求组合数,求所有组合的数字列表的算法

15 回复  |  直到 2 年前

0b4b433679e1fb52f7e2ef4ea492496d?s=32&d=identicon&r=PG

1

28

12 年前

一个简单的方法是创建一个位集,其中的位和数字的位一样多。

在你的例子4中。

然后从0001数到1111,并对集合中有1的每个数字求和:

数字1,4,7,13:

0001 = 13=13

0010 = 7=7

0011 = 7+13 = 20

1111 = 1+4+7+13 = 25

6c862aec243094113c66a9ea22fde390?s=32&d=identicon&r=PG

2

7

12 年前

以下是一个简单的

递归的

在Java中,解决方案看起来像:

public static void main(String[] args)

{

f(new int[] {1,4,7,13}, 0, 0, "{");

}

static void f(int[] numbers, int index, int sum, String output)

{

if (index == numbers.length)

{

System.out.println(output + " } = " + sum);

return;

}

// include numbers[index]

f(numbers, index + 1, sum + numbers[index], output + " " + numbers[index]);

// exclude numbers[index]

f(numbers, index + 1, sum, output);

}

输出:

{ 1 4 7 13 } = 25

{ 1 4 7 } = 12

{ 1 4 13 } = 18

{ 1 4 } = 5

{ 1 7 13 } = 21

{ 1 7 } = 8

{ 1 13 } = 14

{ 1 } = 1

{ 4 7 13 } = 24

{ 4 7 } = 11

{ 4 13 } = 17

{ 4 } = 4

{ 7 13 } = 20

{ 7 } = 7

{ 13 } = 13

{ } = 0

165be4489774038bf3d69198d2d1a021?s=32&d=identicon&r=PG&f=1

3

6

12 年前

最著名的算法需要指数时间。如果有多项式时间算法,那么您可以

subset sum problem

,因此

P=NP problem

.

这里的算法是创建长度等于一组数字基数的位向量。修复枚举

(n_i)

你的一组数字。然后,枚举位向量的所有可能值。对于每个枚举

(e_i)

计算位向量的和

e_i * n_i

.

这里的直觉是,你用一个位向量来表示你的一组数字的子集,并生成这组数字的所有可能子集。当钻头

e_i

等于1,

n_i

在子集中,否则不在。

knuth的taocp的第四卷提供了生成位向量的所有可能值的算法。

8dd90cf47d9cd8d1663bb69f24f2d4a8.png

4

4

12 年前

C:

我想找一个更优雅的东西-但这应该是现在的诀窍…

//Set up our array of integers

int[] items = { 1, 3, 5, 7 };

//Figure out how many bitmasks we need...

//4 bits have a maximum value of 15, so we need 15 masks.

//Calculated as:

// (2 ^ ItemCount) - 1

int len = items.Length;

int calcs = (int)Math.Pow(2, len) - 1;

//Create our array of bitmasks... each item in the array

//represents a unique combination from our items array

string[] masks = Enumerable.Range(1, calcs).Select(i => Convert.ToString(i, 2).PadLeft(len, '0')).ToArray();

//Spit out the corresponding calculation for each bitmask

foreach (string m in masks)

{

//Get the items from our array that correspond to

//the on bits in our mask

int[] incl = items.Where((c, i) => m[i] == '1').ToArray();

//Write out our mask, calculation and resulting sum

Console.WriteLine(

"[{0}] {1}={2}",

m,

String.Join("+", incl.Select(c => c.ToString()).ToArray()),

incl.Sum()

);

}

输出为:

[0001] 7=7

[0010] 5=5

[0011] 5+7=12

[0100] 3=3

[0101] 3+7=10

[0110] 3+5=8

[0111] 3+5+7=15

[1000] 1=1

[1001] 1+7=8

[1010] 1+5=6

[1011] 1+5+7=13

[1100] 1+3=4

[1101] 1+3+7=11

[1110] 1+3+5=9

[1111] 1+3+5+7=16

fae1704291b10722d46c592ffed1815a?s=32&d=identicon&r=PG

5

4

12 年前

下面是一个简单的递归ruby实现:

a = [1, 4, 7, 13]

def add(current, ary, idx, sum)

(idx...ary.length).each do |i|

add(current + [ary[i]], ary, i+1, sum + ary[i])

end

puts "#{current.join('+')} = #{sum}" if current.size > 1

end

add([], a, 0, 0)

哪些版画

1+4+7+13 = 25

1+4+7 = 12

1+4+13 = 18

1+4 = 5

1+7+13 = 21

1+7 = 8

1+13 = 14

4+7+13 = 24

4+7 = 11

4+13 = 17

7+13 = 20

如果不需要在每一步都打印数组,则可以使代码更简单、更快,因为不会创建其他数组:

def add(ary, idx, sum)

(idx...ary.length).each do |i|

add(ary, i+1, sum + ary[i])

end

puts sum

end

add(a, 0, 0)

我不认为你能比这简单得多。

b6d08f3086f5dfa579a59c9f98e5a348?s=32&d=identicon&r=PG

6

3

12 年前

数学解:

{#, Total@#}& /@ Subsets[{1, 4, 7, 13}] //MatrixForm

输出:

{} 0

{1} 1

{4} 4

{7} 7

{13} 13

{1,4} 5

{1,7} 8

{1,13} 14

{4,7} 11

{4,13} 17

{7,13} 20

{1,4,7} 12

{1,4,13} 18

{1,7,13} 21

{4,7,13} 24

{1,4,7,13} 25

edd8638efe875601bfe394a8aea5b16d?s=32&d=identicon&r=PG

7

3

8 年前

这个

Math::Combinatorics

模块还可以处理许多其他情况。即使您不想使用它,文档中也有很多指向有关该问题的其他信息的指针。其他人可能会为你想要的语言推荐合适的库。

#!/usr/bin/perl

use List::Util qw(sum);

use Math::Combinatorics;

my @n = qw(1 4 7 13);

foreach my $count ( 2 .. @n ) {

my $c = Math::Combinatorics->new(

count => $count, # number to choose

data => [@n],

);

print "combinations of $count from: [" . join(" ",@n) . "]\n";

while( my @combo = $c->next_combination ){

print join( ' ', @combo ), " = ", sum( @combo ) , "\n";

}

}

90617e2b1586c87d8007539d8c58ae46?s=32&d=identicon&r=PG

8

2

12 年前

可以使用位向量枚举所有子集。

在for循环中,从0到2到n次方减1(如果不关心空集,则从1开始)。

在每次迭代中,确定设置了哪些位。第n位表示集合的第n个元素。对于每个集合位,取消对集合中适当元素的引用并添加到累积值。

eta:因为这个问题的本质是指数复杂性,所以你可以枚举的集合的大小有一个实际的限制。如果你不需要所有的子集,你可以在“n choose k”中查找枚举k元素子集的方法。

9f45d576164f2ce478158efd65ac13a6?s=32&d=identicon&r=PG

9

1

7 年前

PHP:

这是一个非递归实现。我并不是说这是最有效的方法(这确实是指数2^n-见jasontrue的回复和评论),但它只适用于一小部分元素。我只是想写一些快速的结果。我把图恩的答案作为算法的基础。

$set = array(3, 5, 8, 13, 19);

$additions = array();

for($i = 0; $i < pow(2, count($set)); $i++){

$sum = 0;

$addends = array();

for($j = count($set)-1; $j >= 0; $j--) {

if(pow(2, $j) & $i) {

$sum += $set[$j];

$addends[] = $set[$j];

}

}

$additions[] = array($sum, $addends);

}

sort($additions);

foreach($additions as $addition){

printf("%d\t%s\n", $addition[0], implode('+', $addition[1]));

}

它将输出:

0

3 3

5 5

8 8

8 5+3

11 8+3

13 13

13 8+5

16 13+3

16 8+5+3

18 13+5

19 19

21 13+8

21 13+5+3

22 19+3

24 19+5

24 13+8+3

26 13+8+5

27 19+8

27 19+5+3

29 13+8+5+3

30 19+8+3

32 19+13

32 19+8+5

35 19+13+3

35 19+8+5+3

37 19+13+5

40 19+13+8

40 19+13+5+3

43 19+13+8+3

45 19+13+8+5

48 19+13+8+5+3

例如,这种情况可以是一组用于计算的阻力带。假设你得到5个带,每个带有不同的阻力,用磅表示,你可以组合带来总结总阻力。带电阻分别为3、5、8、13和19磅。这个集合提供32(2^5)个可能的配置,减去0。在本例中,算法首先返回按总阻力上升排序的数据,有利于有效的频带配置,对于每个配置,频带按阻力下降排序。

330fea5fe1ac373b9a265efeaaebf160?s=32&d=identicon&r=PG

10

0

12 年前

这不是生成和的代码,而是生成排列。就你而言:

1;1,4;1,7;4,7;1,4,7;…

如果周末我有时间,如果有趣的话,我可以修改这个来计算出总数。

T[] arr = â¦;

var subsets = from m in Enumerable.Range(0, 1 << arr.Length)

select

from i in Enumerable.Range(0, arr.Length)

where (m & (1 << i)) != 0

select arr[i];

8dd90cf47d9cd8d1663bb69f24f2d4a8.png

11

0

12 年前

如果你想避免维护费用的话,你可能会对查看gnu科学图书馆感兴趣。对较长序列求和的实际过程将变得非常昂贵(比在一个步骤的基础上生成一个排列更为昂贵),大多数体系结构都有simd/向量指令,可以提供相当可观的速度(我将提供此类实现的示例,但我不能发布URL)。

03eb9b18da39d3069d068e9a8ebf0029?s=32&d=identicon&r=PG

12

0

9 年前

谢谢扎克,

我正在创建一个银行对帐解决方案。我把您的代码放到jsbin.com中进行了一些快速测试,并用javascript生成了以下代码:

function f(numbers,ids, index, sum, output, outputid, find )

{

if (index == numbers.length){

var x ="";

if (find == sum) {

y= output + " } = " + sum + " " + outputid + " }
" ;

}

return;

}

f(numbers,ids, index + 1, sum + numbers[index], output + " " + numbers[index], outputid + " " + ids[index], find);

f(numbers,ids, index + 1, sum, output, outputid,find);

}

var y;

f( [1.2,4,7,13,45,325,23,245,78,432,1,2,6],[1,2,3,4,5,6,7,8,9,10,11,12,13], 0, 0, '{','{', 24.2);

if (document.getElementById('hello')) {

document.getElementById('hello').innerHTML = y;

}

我需要它生成一个从下一个匹配号码中排除的id列表。

我将使用vb.net发布我的最终解决方案

7ed726123702f675c961eaa18ee403ef?s=32&d=identicon&r=PG&f=1

13

0

5 年前

v=[1,2,3,4]#variables to sum

i=0

clis=[]#check list for solution excluding the variables itself

def iterate(lis,a,b):

global i

global clis

while len(b)!=0 and i

a=lis[i]

b=lis[i+1:]

if len(b)>1:

t=a+sum(b)

clis.append(t)

for j in b:

clis.append(a+j)

i+=1

iterate(lis,a,b)

iterate(v,0,v)

它是用python编写的。其思想是将列表分成一个整数和一个例如[1,2,3,4]的列表,并将其分成1,[2,3,4]。现在,我们通过将剩余列表的整数和和相加来附加总和,并且取每个单独的和,即1,2;1,3;1,4。清单现在应该是[1+2+3+4,1+2,1+3,1+4],然后我们递归地调用新列表,即现在int=2,list=[3,4]。检查表现在将追加[2+3+4,2+3,2+4]相应地,我们追加检查表,直到列表为空。

8dd90cf47d9cd8d1663bb69f24f2d4a8.png

14

0

4 年前

set是和的集合,list是原始数字的列表。

它的Java。

public void subSums() {

Set resultSet = new HashSet();

for(long l: list) {

for(long s: set) {

resultSet.add(s);

resultSet.add(l + s);

}

resultSet.add(l);

set.addAll(resultSet);

resultSet.clear();

}

}

6f56b22655fa63b7f8124a1d2c253ca3?s=32&d=identicon&r=PG

15

0

2 年前

public static void main(String[] args) {

// this is an example number

long number = 245L;

int sum = 0;

if (number > 0) {

do {

int last = (int) (number % 10);

sum = (sum + last) % 9;

} while ((number /= 10) > 0);

System.err.println("s = " + (sum==0 ? 9:sum);

} else {

System.err.println("0");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值