Project Euler 24使用java逻辑错误

Project Euler 24使用java逻辑错误,java,functional-programming,permutation,Java,Functional Programming,Permutation,我正在努力解决问题24 我和euler在这个问题上花了很多时间,但仍然没有成功 但现在问题不在这里了,我想知道我的方法有什么问题 我使用简单排列来获得最左边的数字,并从要查找的项中获得剩余值(通过减法,即使用模运算) Java代码: import java.util.ArrayList; public class LexOrder { public static int factorial(int num) { int res=1; if(num <= 1)

我正在努力解决问题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);
}
}
import java.util.ArrayList;
公共秩序
{
公共静态整数阶乘(整数)
{
int res=1;
如果(数字1)
{
res*=num--;
}
返回res;
}
公共静态ArrayList addValue(整数位数,ArrayList al)
{
内部温度=0,计数=0;
while(计数索引和索引>=0)
{
System.out.println(“索引已验证”);
}
其他的
{
System.out.println(“索引超出范围”);
系统出口(0);
}
整数位数,计数=1;
while(索引!=0)
{
数字=(指数/阶乘(数字-计数));
al=附加值(数字,al);
指数=(指数%阶乘(数字计数));
如果(索引==0)
打破
计数++;
}
//按升序增加价值。
内部温度=0;
while(al.size()
输出: [2,7,8,3,9,1,4,5,6,0]

输出应为:
2783915460

问题在于方法
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.
}
公共静态ArrayList addValue(整数位数,ArrayList al){
List List=newarraylist(Arrays.asList(0,1,2,3,4,5,6,7,8,9));
列表。移除所有(al);
所有添加(列表获取(数字));
return al;//实际上这应该是一个void方法,因为al是一个参数。
}

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

因此,在无法告诉您确切数字的情况下,我可以看出您管理数字的方式出了问题:

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

int val = digit+temp;

它很有效,而且比我的答案好得多,因为它尽可能多地保留了OP的原始代码。@Linus谢谢!上帝保佑你:)事实上,我对这个问题的设计是从一开始就保留这个未使用的数字列表,然后在执行时删除它(因为在这种情况下,它们是符号而不是数量)。所以这是低效的,因为它符合现有的方法头,但总的来说这是一个非常好的主意。@Linus我完全同意。“如果我是从头开始写的话,那将是非常不同的。”保尔·伯丁顿,谢谢!上帝保佑你:)
int val=0, count=0;
while( count < digit)
{   if( !al.contains(val++) )
        count++;
}