Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';我的搜索算法对Euler 31项目有效吗?_Java_Algorithm_Search_Data Structures_Tree - Fatal编程技术网

Java 为什么';我的搜索算法对Euler 31项目有效吗?

Java 为什么';我的搜索算法对Euler 31项目有效吗?,java,algorithm,search,data-structures,tree,Java,Algorithm,Search,Data Structures,Tree,问题31 在英国,货币由英镑、英镑、便士、便士和英镑组成 一般流通的硬币有八种:1便士、2便士、5便士、10便士、20便士, 50便士、1英镑(100便士)和2英镑(200便士)。有可能在未来赚2英镑 以下方式:1×1+1×50p+2×20p+1×5p+1×2p+3×1p有多少 可以使用任意数量的硬币以不同的方式制造2英镑 static int[]nums={200100,50,20,10,5,2,1}; 静态整数大小=nums.length; 静态HashMap pivots=新HashMap

问题31

在英国,货币由英镑、英镑、便士、便士和英镑组成 一般流通的硬币有八种:1便士、2便士、5便士、10便士、20便士, 50便士、1英镑(100便士)和2英镑(200便士)。有可能在未来赚2英镑 以下方式:1×1+1×50p+2×20p+1×5p+1×2p+3×1p有多少 可以使用任意数量的硬币以不同的方式制造2英镑

static int[]nums={200100,50,20,10,5,2,1};
静态整数大小=nums.length;
静态HashMap pivots=新HashMap();
公共静态整数校验和(哈希映射数据透视){
int目标=200;
整数和=0;
for(整数键:pivots.keySet()){
int pivot=pivots.get(键);
总和+=nums[支点];
如果(总和>目标)返回1;
}
如果(总和<目标)返回-1;
返回0;
}
公共静态void shift(HashMap pivots,int pivot_节点){
if(pivots.size()+nums[pivots.get(1)]==201&&pivots.get(1)!=0){
int p_1_value=pivots.get(1);//此部分检查当前节点(即第一个节点)是否
//已惠及所有1名儿童。
//这意味着是时候移动根节点了。
pivots.clear();
pivots.put(1,p_1_值);
移位(枢轴,1);
返回;
}
if(pivots.get(pivot\u节点)!=size-1){
pivots.put(pivot\u节点,pivots.get(pivot\u节点)+1);
}
否则{
移位(枢轴,枢轴节点-1);
}
}
公共静态void分支(HashMap枢轴){
pivots.put(pivots.size()+1,pivots.get(pivots.size());
}
公共静态整型搜索(){
int bool=校验和(枢轴);
int n=0;
整数计数=0;
而(n<25){
计数++;
如果(bool==0){
n++;//如果和等于200,我们移动最后一个
//转到下一个较低的数字。
shift(pivots,pivots.size());
}如果(bool==-1){
分支(pivots);//如果总和小于200,则使用最后一个pivot的值创建一个新的pivot。
}如果(bool==1){
shift(pivots,pivots.size());//如果总和大于200,
//我们将切换到最后一个轴心到下一个较低的数字。
}
bool=校验和(枢轴);
}
返回n;
}
公共静态void main(字符串[]args){
枢轴。put(1,0);
int n=搜索();
System.out.print(“\n\n------\n\n”+”n:“+n”);
}
这是一种算法,用于搜索集合的组合,这些组合相加为一个目标。这有点像不使用树的深度优先树搜索。每个轴代表“树”上的节点。方法将节点的值更改为下一个较低的值。方法的作用是:创建一个与上一个节点值相同的新节点。checkSum()方法检查轴的和是否为目标200

正确答案应该是73000左右。但我的算法只返回大约300种方式。 我不知道为什么会发生这种情况,因为我的算法应该达到每一个可能的组合等于200

这是我的算法工作原理的可视化:


您的搜索算法无法找到构成2英镑的所有可能硬币组合,因为您只是将“最后一个轴心”移到下一个较低的数字,而您也应该考虑最后一个轴心之前的项目

您的算法将找到以下组合:
100,50,20,20,5,2,2,1
但不是这个:
100,20,20,20,10,5,2,2,1

第二个组合中没有值50,但您的算法仅将硬币值从向后分解为向前-即,在以下所有“支点”都为1之前,它永远不会分解为50。您可以很容易地看到,如果每次计数器n递增时都打印
HashMap pivots

您可以尝试通过将代码修改为
shift()
来修复代码,不仅使用最后一个轴,还使用所有不同的前一个轴。但是,这样做会创建大量重复项,因此需要保留一个找到的不同组合的列表

解决问题31的另一种方法是使用动态规划。当涉及到可以分解为更小的问题时,动态规划是最好的。例如,相同问题的解决方案,但在哪里
target=2
可用于解决
target=5
的问题,可用于解决
target=10
的问题,依此类推


祝你好运

您还应该添加问题陈述并链接到它(不是每个人都能记住所有Project Euler问题)
static int[] nums = {200,100,50,20,10,5,2,1};
static int size = nums.length;
static HashMap<Integer,Integer> pivots = new HashMap<>();

public static int checkSum(HashMap<Integer,Integer> pivots){

    int target = 200;
    int sum = 0;

    for(Integer key: pivots.keySet()){
        int pivot = pivots.get(key);
        sum +=  nums[pivot];
        if(sum > target) return 1;
    }

    if(sum < target) return -1;

    return 0;
}

public static void shift(HashMap<Integer,Integer> pivots, int pivot_node){

    if(pivots.size() + nums[pivots.get(1)] == 201 && pivots.get(1) != 0){

        int p_1_value = pivots.get(1);   //this part checks whether the  current node(which is the first node)                                                                  
                                  //has reached children of all 1. 
                             //Which means it's time to shift the root node.
        pivots.clear();
        pivots.put(1 , p_1_value);
        shift(pivots, 1);
        return;
    }

    if(pivots.get(pivot_node) != size - 1) {
        pivots.put(pivot_node, pivots.get(pivot_node) + 1);
    }
    else{
        shift(pivots , pivot_node - 1);
    }

}



public static void branch(HashMap<Integer,Integer> pivots){
    pivots.put(pivots.size() + 1, pivots.get(pivots.size()));
}

public static int search(){
    int bool = checkSum(pivots);

    int n = 0;
    int count = 0;

    while(n < 25) {
        count++;
        if (bool == 0) {
            n++;     // if the sum is equal to 200, we shift the last 
                     //pivot to the next lower number.               
            shift(pivots, pivots.size());

        }else if (bool == -1) {   
            branch(pivots); //if the sum is less than 200, we make a new pivot with value of the last pivot.
        }else if (bool == 1) {    
            shift(pivots, pivots.size());  //if the sum is greater than 200,
                                          //we shift to the last pivot to the next lower number. 
        }
        bool = checkSum(pivots);
    }
    return n;
}

public static void main(String[] args){

    pivots.put(1,0);

    int n = search();

    System.out.print("\n\n------------\n\n"+ "n: " + n);

}