【问题标题】:How to find 3 numbers in all combinations possible that in a sum the result equals n?如何在所有可能的组合中找到总和等于 n 的 3 个数字?
【发布时间】:2014-09-26 06:54:54
【问题描述】:

我在 Authomata 工作,需要三个等于 n 的数字 例如,如果 n = 2,我需要的数字是:

200
020
002
110
101
011

组合是否重复并不重要。

如果 n = 3 我需要:

300
030
003
210
201
021
120
012
102
111

所以我读到这类似于数论中的分区,但我可以得到只有 3 个数字给我目标值 (n) 的特殊情况。 (代码来自我在这里得到的一个例子)

package automata2;

import java.util.ArrayList;

/**
 *
 * @author jaime
 */
public class Automata2 {


    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        int N = 14;
        partition(N);
    }

    public static void partition(int n) {
        partition(n, n, "");
    }
    public static void partition(int n, int max, String prefix) {
        if (n == 0) {
            System.out.println(prefix);
            return;
        }

        for (int i = Math.min(max, n); i >= 1; i--) {
            partition(n-i, i, prefix + " " + i);
        }
    }
}

但我只需要三个数字的所有组合,而不是从 14 到 1 1 1 1 1 1 1 1 1 1 1 1 1 1 的所有组合

【问题讨论】:

  • 这里的蛮力有什么问题?
  • 我不太确定该怎么做,我尝试了一个 switch 结构,但条件相当困难,我是编程新手,很抱歉,如果这是一个简单的问题。
  • 看起来您需要 2 个嵌套的 for 循环来生成所有选项,并且对于每个选项,您可以测试节点是否满足您的条件。之所以称为蛮力,是因为您生成所有可能的解决方案只是为了找到您真正想要的少数解决方案。
  • 你能告诉我这个算法吗?
  • @Leo 实际上,两个循环就足够了。第三个数字更适合计算为N-n1-n2。额外的好处:效率更高。

标签: java math partition


【解决方案1】:

生成数字的算法是这样的。此代码将生成所有编号。

public class main1 {
    public static void main(String args[]) {

        int N = 5, n1, n2, n3;

        for (n1 = 0; n1 <= N; n1++) {
            for (n2 = 0; n2 <= N; n2++) {
                for (n3 = 0; n3 <= N; n3++) {
                    if ( (n1+n2+n3)==N ) {
                        System.out.println(n1 + " " + n2 + " " + n3);
                    }
                }
            }
        }
    }
}

您可以做的是代替System.out.println(n1+" "+n2+" "+n3),您可以将数字存储在数组列表中然后排列列表。

【讨论】:

  • 这只会分成3个数字。
  • 三个数字的分区就是我需要的
  • 您也可以使用数字 n1 n2 n3 并将数字转换为字符串,但我认为使用数组列表更好。
  • 您可以只使用两个嵌套循环并执行System.out.println(i + " " + j + " " + (N-i-j) ); 以节省复杂度。
【解决方案2】:

一种解决方案是检查数字的数量是三,然后打印出来

    public static void main(String[] args) {
        int N = 14;
        partition(N);
    }

    public static void partition(int n) {
        partition(n, n, "");
    }
    public static void partition(int n, int max, String prefix) {
        // added condition here
        if (n == 0 && prefix.trim().split(" ").length == 3) {
            System.out.println(prefix);
            return;
        }

        for (int i = Math.min(max, n); i >= 1; i--) {
            partition(n-i, i, prefix + " " + i);
        }
    }

输出:

 12 1 1
 11 2 1
 10 3 1
 10 2 2
 9 4 1
 9 3 2
 8 5 1
 8 4 2
 8 3 3
 7 6 1
 7 5 2
 7 4 3
 6 6 2
 6 5 3
 6 4 4
 5 5 4

使用嵌套循环 - 仅适用于 3 位数解决方案

    int N = 14;
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= N; j++) {
            for (int k = 0; k <= N; k++) {
                if (i + j + k == N) {
                    System.out.println(i + " " + j + " " + k);              
                }           
            }       
        }   
    }

输出:

0 0 14
0 1 13
0 2 12
0 3 11
0 4 10
0 5 9
0 6 8
0 7 7
0 8 6
0 9 5
0 10 4
0 11 3
0 12 2
0 13 1
0 14 0
1 0 13
1 1 12
1 2 11
1 3 10
1 4 9
1 5 8
1 6 7
1 7 6
1 8 5
1 9 4
1 10 3
1 11 2
1 12 1
1 13 0
2 0 12
2 1 11
2 2 10
2 3 9
2 4 8
2 5 7
2 6 6
2 7 5
2 8 4
2 9 3
2 10 2
2 11 1
2 12 0
3 0 11
3 1 10
3 2 9
3 3 8
3 4 7
3 5 6
3 6 5
3 7 4
3 8 3
3 9 2
3 10 1
3 11 0
4 0 10
4 1 9
4 2 8
4 3 7
4 4 6
4 5 5
4 6 4
4 7 3
4 8 2
4 9 1
4 10 0
5 0 9
5 1 8
5 2 7
5 3 6
5 4 5
5 5 4
5 6 3
5 7 2
5 8 1
5 9 0
6 0 8
6 1 7
6 2 6
6 3 5
6 4 4
6 5 3
6 6 2
6 7 1
6 8 0
7 0 7
7 1 6
7 2 5
7 3 4
7 4 3
7 5 2
7 6 1
7 7 0
8 0 6
8 1 5
8 2 4
8 3 3
8 4 2
8 5 1
8 6 0
9 0 5
9 1 4
9 2 3
9 3 2
9 4 1
9 5 0
10 0 4
10 1 3
10 2 2
10 3 1
10 4 0
11 0 3
11 1 2
11 2 1
11 3 0
12 0 2
12 1 1
12 2 0
13 0 1
13 1 0
14 0 0

【讨论】:

    【解决方案3】:

    通过跳过不必要的分区逐一检查。

    public static void main(String[] args) {
        int n =14;
        ArrayList<String> temp = partition(n);
        for (int z =0; z<temp.size(); z++){
            System.out.println(temp.get(z));
        }
    
    }
    public static ArrayList<String> partition(int n){
        ArrayList<String> out = new ArrayList<String>();
        int a=0, b=0, c=0;
        for (c=n; c>=0; c--){
            for(int j =a; j<=n;j++){
                if((c+j)>n){
                    j=n+1;
                    continue;
                }
                for (int i=b;i<=n; i++){
                    int p=c+j+i;
                    if (p>n) i=n+1;
                    if(p==n){
                        String s = Integer.toString(c)+" "+ Integer.toString(j)+" "+ Integer.toString(i); 
                        out.add(s);
                    }
                }
    
            }
    
        }
        return out;
    }
    

    输出:

    14 0 0
    13 0 1
    13 1 0
    12 0 2
    12 1 1
    12 2 0
    11 0 3
    11 1 2
    11 2 1
    11 3 0
    10 0 4
    10 1 3
    10 2 2
    10 3 1
    10 4 0
    9 0 5
    9 1 4
    9 2 3
    9 3 2
    9 4 1
    9 5 0
    8 0 6
    8 1 5
    8 2 4
    8 3 3
    8 4 2
    8 5 1
    8 6 0
    7 0 7
    7 1 6
    7 2 5
    7 3 4
    7 4 3
    7 5 2
    7 6 1
    7 7 0
    6 0 8
    6 1 7
    6 2 6
    6 3 5
    6 4 4
    6 5 3
    6 6 2
    6 7 1
    6 8 0
    5 0 9
    5 1 8
    5 2 7
    5 3 6
    5 4 5
    5 5 4
    5 6 3
    5 7 2
    5 8 1
    5 9 0
    4 0 10
    4 1 9
    4 2 8
    4 3 7
    4 4 6
    4 5 5
    4 6 4
    4 7 3
    4 8 2
    4 9 1
    4 10 0
    3 0 11
    3 1 10
    3 2 9
    3 3 8
    3 4 7
    3 5 6
    3 6 5
    3 7 4
    3 8 3
    3 9 2
    3 10 1
    3 11 0
    2 0 12
    2 1 11
    2 2 10
    2 3 9
    2 4 8
    2 5 7
    2 6 6
    2 7 5
    2 8 4
    2 9 3
    2 10 2
    2 11 1
    2 12 0
    1 0 13
    1 1 12
    1 2 11
    1 3 10
    1 4 9
    1 5 8
    1 6 7
    1 7 6
    1 8 5
    1 9 4
    1 10 3
    1 11 2
    1 12 1
    1 13 0
    0 0 14
    0 1 13
    0 2 12
    0 3 11
    0 4 10
    0 5 9
    0 6 8
    0 7 7
    0 8 6
    0 9 5
    0 10 4
    0 11 3
    0 12 2
    0 13 1
    0 14 0
    

    【讨论】:

      【解决方案4】:

      导入 java.util.Scanner;

      公共类 ExampleMinNumber {

      public static void main(String args[]) 
      {
      
          System.out.println("enter number which comibnation of sum you want");
          Scanner input=new Scanner(System.in);
          int num=input.nextInt();
          for(int i=0;i<=3;i++)
          {
              int sum=0;
              for(int j=0;j<=3;j++)
              {
                  for(int k=0;k<=3;k++)
                  {
                      sum=i+j+k;
                      if(sum==num)
                      {
                          System.out.println(+i+""+j+""+k);
                      }
      
                  }
              }
          }
      
          input.close();  
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-01-06
        • 2019-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-05
        相关资源
        最近更新 更多