求出一堆数中和等于m的所有可能组合 JAVA

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求出一堆数中和等于m的所有可能组合 JAVA相关的知识,希望对你有一定的参考价值。

输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.
最好是用JAVA写,告诉思路也可以。帮帮我吧!

m StrOut() As String

Private Sub Command1_Click()
Dim Num() As Variant
Dim GroupMax As Long
Dim i As Long, j As Long, l As Long, m As Long, n As Long
Dim lngTemp() As Long
Dim TT As Long
GroupMax = 5
ReDim lngTemp(GroupMax)
'在此对数据进行初始化
Num = Array(1, 2, 3, 4, 5, 6, 7, 8)
TT = 1
For i = UBound(Num) + 1 To GroupMax - 1 Step -1
TT = TT * i
Next

For i = 2 To GroupMax
TT = TT / i
Next
ReDim StrOut(TT)

For i = 1 To TT
StrOut(i) = ""
Next

For i = 0 To UBound(Num)
For j = 0 To UBound(Num)
For l = 0 To UBound(Num)
For m = 0 To UBound(Num)
For n = 0 To UBound(Num)
lngTemp(1) = Num(i)
lngTemp(2) = Num(j)
lngTemp(3) = Num(l)
lngTemp(4) = Num(m)
lngTemp(5) = Num(n)
Call strAdd(lngTemp)
Next
Next
Next
Next
Next

Open "D:\12345.txt" For Append As #1
For i = 1 To UBound(StrOut)
Print #1, StrOut(i)
Next
Close (1)
MsgBox "OK"
End Sub

Private Sub strAdd(lngTemp() As Long)
Dim GroupMax As Long
Dim i As Long, j As Long, TT As Long
Dim StrTemp As String
GroupMax = UBound(lngTemp)

For i = 1 To GroupMax
For j = i + 1 To GroupMax
If lngTemp(j) = lngTemp(i) Then
Exit Sub
End If
If lngTemp(j) < lngTemp(i) Then
TT = lngTemp(j)
lngTemp(j) = lngTemp(i)
lngTemp(i) = TT
End If
Next
Next
StrTemp = ""
For i = 1 To GroupMax
StrTemp = StrTemp & lngTemp(i)
Next

For i = 1 To UBound(StrOut)
If StrOut(i) = "" Then Exit For
If StrOut(i) = StrTemp Then Exit Sub
Next
StrOut(i) = StrTemp

End Sub
另外,站长团上有产品团购,便宜有保证
参考技术A 这题费了老劲了,可用递归来做。原理我就不说了,能看明白就看吧。 算法如下(可能算法不优):

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test

static List<List<Integer>> printUnique = new ArrayList<List<Integer>>();
public static void main(String[] args)
cal(18,20,new ArrayList<Integer>());
System.out.println("*********************** 组合结果");
for(List<Integer> result : printUnique)
for(Integer num : result)
System.out.print(num + " ");

System.out.println("");


public Test()



public static void cal(Integer maxNum, Integer result, List<Integer> chain)
if(result>=2 * maxNum) return;
if(result<2) return;

Integer minNum =0;
if(result > maxNum)
minNum = result - maxNum;


if(result == maxNum)
minNum = 1;
maxNum = maxNum -1;


if(result <maxNum)
minNum =1 ;
maxNum = result -1;


while(minNum < maxNum)
Integer first = minNum ++;
Integer last = maxNum --;
if(first==last) break;
if(chain.contains(first) || chain.contains(last)) continue;

List<Integer> print = new ArrayList<Integer>(chain);
print.add(first);
print.add(last);
for(Integer i :print)
System.out.print(i +" ");

System.out.println("");

Collections.sort(print);
if( !printUnique.contains(print))
printUnique.add(print);


List<Integer> chain1 = new ArrayList<Integer>(chain);
//Collections.copy(chain1, chain);
chain1.add(first);
cal(last,last,chain1);

List<Integer> chain2 = new ArrayList<Integer>(chain);
chain2.add(last);
cal(first,first,chain2);




本回答被提问者采纳

编程求解,输入两个整数n和m,从数列1,2,3,……n中随意取几个数,使其和等于m。要求将所有的可能组合列出来

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    private static LinkedList<Integer> list = new LinkedList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            findSum(m, 1, n);
        }
    }

    public static void findSum(int sum, int start, int end) {
        if (sum < 0 || start > end + 1)
            return;
        else if (sum == 0){
            list.forEach(x ->System.out.print(x + " "));
            System.out.println();
            return;
        }
        if (sum >= start) {
            list.push(start);
            findSum(sum - start, start + 1, end);
            list.pop();
            findSum(sum, start+1, end);
        }
    }
}

 

以上是关于求出一堆数中和等于m的所有可能组合 JAVA的主要内容,如果未能解决你的问题,请参考以下文章

编程求解,输入两个整数n和m,从数列1,2,3,……n中随意取几个数,使其和等于m。要求将所有的可能组合列出来

java从一堆数中找出和已知数最接近的数

LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

使用java,列举所有给定数组中和为定值的组合

如果 a+b+c=1000,且 a^2+b^2=c^2(a,b,c 为自然数),如何求出所有abc可能的组合?Java/JavaScript/C/Python耗时对比

如何从一堆数中选出若干个数,使其和等于给定的数?