首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >计算必须产生另一个数字的集合数字的所有可能组合

计算必须产生另一个数字的集合数字的所有可能组合
EN

Stack Overflow用户
提问于 2021-11-24 09:22:07
回答 1查看 47关注 0票数 0

我不确定你是否通过阅读标题来理解这个问题,因为我不知道如何在标题中很好地解释它。

我必须编写以下代码:有一个方法有三个参数-第一个数字表示距离,第二个和第三个表示跳跃长度。跳跃长度必须在所有可能的组合中求和,以最终等于距离,然后必须打印出组合的数量。

例如: 7,3,4。距离= 7,跳跃长度1= 3,跳跃长度2 = 4。这里有两种可能的组合来达到7:3+ 4和4+3 =>输出=2。

或者8,2,4:距离= 8,跳跃长度1= 2,跳跃长度2= 4。这里有5种可能的组合:4+ 4,2+2+4,2+4+2,4+2+2,2+2+2+2 => output =5。

我整个晚上基本上都在努力解决这个问题,但我并没有真正取得进展。有人愿意帮我一点忙吗?我才上一学期,我几乎一无所知。

EN

回答 1

Stack Overflow用户

发布于 2021-11-24 13:01:48

您必须遵循的顶级算法是:

  • 为每个输入维护一棵树
  • 不断添加其中一个输入,直到您达到距离
  • 仅当距离匹配时才添加节点作为子节点(即sum = distance)。这需要递归循环。

但是,当您在代码中实现它时,它可能看起来非常不同。如下所示:

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Main{
    private int distance;
    private int a;
    private int b;
    
    public Main( int distance, int a, int b ){
        this.distance = distance;
        this.a = a;
        this.b = b;
    }
    
    /* Represents a node in the tree. Maintains the sum till now in the path, and the value for the current node. */
    private static class Node{
        int sumTillNow;
        int val;
        List<Node> children = new ArrayList<>();
        
        Node( int sumTillNow, int val ){
            super();
            this.sumTillNow = sumTillNow;
            this.val = val;
        }
        
        @Override
        public String toString(){
            String s = String.valueOf( this.val );
            if( !this.children.isEmpty() ) s = s + "," + this.children;
            return s;
        }
        
        /* This is the most difficult part - traversal in a way that each path is retrieved individually. */
        public List<List<Integer>> traverse( int distance, List<Integer> list ) {
            list.add( this.val );
            
            if( this.children.isEmpty() ) return Arrays.asList( list );
            if( this.children.size() == 1 ) return this.children.get( 0 ).traverse( distance, list );
            
            List<List<Integer>> listForReturning = new ArrayList<>();
            if( this.children.size() > 1 ) for( Node ch : this.children ) {
                List<Integer> copy = new ArrayList<>( list );
                List<List<Integer>> chList = ch.traverse( distance, copy );
                if( chList != null ) listForReturning.addAll( chList );
            }
            
            return listForReturning;
        }
    }
    
    public static void main( String[] args ) {
        List<List<Integer>> allCombs = new Main( 10, 4, 2 ).comp();
        for( List<Integer> comb : allCombs ) System.out.println( comb );
    }
    
    private List<List<Integer>> comp(){
        Node aNode = new Node( a, a );
        Node bNode = new Node( b, b );
        
        /* Handle the special cases of input here. */
        if( a > distance && b > distance ) return null;
        if( a == 0 && b == 0 ) return null;
        if( a == distance ) {
            aNode.sumTillNow = distance;
            aNode.val = a;
        }
        if( b == distance ) {
            aNode.sumTillNow = distance;
            aNode.val = b;
        }
        
        /* Run the computation once starting with the first input and then with the next input. */
        if( aNode.sumTillNow < distance ) loop( aNode );
        if( bNode.sumTillNow < distance ) loop( bNode );
        
        return traverse( aNode, bNode );
    }

    private boolean loop( Node node ){
        if( node.sumTillNow < distance ) {
            addIfRemaining( node, a );
            addIfRemaining( node, b );
            
            return true;
        }
        else if( node.sumTillNow == distance ) return true;
        else return false;
    }

    private void addIfRemaining( Node node, int addition ){
        if( node.sumTillNow + addition <= distance ) {
            Node n = new Node( node.sumTillNow + addition, addition );
            if( n.sumTillNow <= distance ) {
                boolean good = loop( n );
                if( good ) node.children.add( n );
            }
        }
    }

    private List<List<Integer>> traverse( Node aNode, Node bNode ){
        List<List<Integer>> allPaths = new ArrayList<>();
        allPaths.addAll( aNode.traverse( this.distance, new ArrayList<>() ) );
        allPaths.addAll( bNode.traverse( this.distance, new ArrayList<>() ) );
        
        return allPaths;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70093508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档