我不确定你是否通过阅读标题来理解这个问题,因为我不知道如何在标题中很好地解释它。
我必须编写以下代码:有一个方法有三个参数-第一个数字表示距离,第二个和第三个表示跳跃长度。跳跃长度必须在所有可能的组合中求和,以最终等于距离,然后必须打印出组合的数量。
例如: 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。
我整个晚上基本上都在努力解决这个问题,但我并没有真正取得进展。有人愿意帮我一点忙吗?我才上一学期,我几乎一无所知。
发布于 2021-11-24 13:01:48
您必须遵循的顶级算法是:
但是,当您在代码中实现它时,它可能看起来非常不同。如下所示:
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;
}
}https://stackoverflow.com/questions/70093508
复制相似问题