【问题标题】:Project Euler 24 using java Logical Error项目 Euler 24 使用 java 逻辑错误
【发布时间】:2015-11-23 03:03:51
【问题描述】:

我正在尝试解决问题 24 https://projecteuler.net/problem=24 euler 项目的我和我在这个问题上花了很多时间但仍然没有成功。

但现在不是问题了,我想知道我的方法有什么问题。

我正在使用简单的排列来获取最左边的数字并从要找到的术语中获取剩余值(通过减法,即使用模运算符)。

Java 代码:

import java.util.ArrayList;

public class LexOrder
{

public static int factorial(int num)
{
    int res=1;
    if(num <= 1)
        return 1;

    while(num > 1)  
    {
        res *= num--;   
    }
    return res;
}

public static ArrayList<Integer> addValue(int digit, ArrayList<Integer> al)
{
    int temp=0, count=0;    
    while( count <= digit )
    {
        if( al.contains(count) )
            temp++;
     count++;
    }

    int val = digit+temp;

    //checking weather the new number exists in the ArrayList or not.
    while(true)
    {
        if(! al.contains(val) )
        {
            al.add(val);
            break;
        }
        else
        {
            val++;
        }
    }
    return al;
}

public static void main(String args[])
{
    ArrayList<Integer> al = new ArrayList<Integer>();   
    int index = 999999; 
    int numOfDigit = 10;

    if( factorial( numOfDigit ) > index && index >= 0)
    {
            System.out.println("Index Validated");
    }
    else
    {
            System.out.println("Index out of bounds");
            System.exit(0);
    }

    int digit, count=1;
    while( index !=0  )
    {
        digit = ( index / factorial( numOfDigit - count ) );
        al = addValue(digit, al);

        index = ( index % factorial( numOfDigit - count ) );

        if(index == 0)
            break;
        count++;
    }

    // Adding value in ascending order.
    int temp =0;
    while( al.size() < numOfDigit )
    {

        if(!al.contains(temp))
            al.add(temp);

    temp++;
    }

    System.out.println(al);
}
}

输出: [2, 7, 8, 3, 9, 1, 4, 5, 6, 0]

输出应该是: 2783915460

【问题讨论】:

    标签: java functional-programming permutation


    【解决方案1】:

    因此,由于无法告诉您确切的数字,我可以说您管理数字的方式有些问题:

    int temp=0, count=0;    
    while( count <= digit )
    {
        if( al.contains(count) )
            temp++;
     count++;
    }
    
    int val = digit+temp;
    

    这种方法尤其无法检查某些数字(尤其是digitdigit+temp 之间的数字)是否已经在您的数组列表中。

    我能够通过计算未使用的元素数量来解决它:

    int val=0, count=0;
    while( count < digit)
    {   if( !al.contains(val++) )
            count++;
    }
    

    【讨论】:

    • 它有效,而且比我的回答要好得多,因为它尽可能多地保留了 OP 的原始代码。
    • @Linus 谢谢!上帝保佑你:)
    【解决方案2】:

    问题在于方法addValue。正确编写此方法的一种简单但低效的方法是

    public static ArrayList<Integer> addValue(int digit, ArrayList<Integer> al) {
        List<Integer> list = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
        list.removeAll(al);
        al.add(list.get(digit));
        return al;  // Really this should be a void method as al is a parameter.
    }
    

    通过这些更改,输出符合您的要求。

    【讨论】:

    • 实际上,我对这个问题的设计是从一开始就保留这个未使用数字的列表,然后在你去的时候删除它(因为在这种情况下它们是符号而不是数量)。所以这只是效率低下,因为它符合现有的方法头,但总的来说这是一个非常好的想法。
    • @Linus 我完全同意。如果我从头开始写它会很不一样。
    • @PaulBoddington 谢谢!上帝保佑你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多