用java编写生成5位数的数(数字中不包含4),并且每次生成的都不能和之前生成的有重复
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/15 14:48:32
用java编写生成5位数的数(数字中不包含4),并且每次生成的都不能和之前生成的有重复
用java编写生成5位数的数(数字中不包含4),并且每次生成的都不能和之前生成的有重复
用java编写生成5位数的数(数字中不包含4),并且每次生成的都不能和之前生成的有重复
package zhidao;
import java.util.Arrays;
import java.util.LinkedList;
public class RecursionSubNSort
{
public static void main ( String[] args )
{
String[] A = { "1", "2", "3", "5", "6", "7", "8", "9", "0" };
LinkedList<String[]> list = new LinkedList<String[]> ();
recursionSub (list, 5, A, 0, -1);
System.out.println ("符合条件的5位数有:");
for ( String[] strings : list )
{
System.out.println (Arrays.toString (strings).replaceAll ("[\\[\\],\\s]", ""));
}
}
private static LinkedList<String[]> recursionSub ( LinkedList<String[]> list, int count, String[] array, int ind,
int start, int... indexs )
{
start++;
if (start > count - 1)
{
return null;
}
if (start == 0)
{
indexs = new int[array.length];
}
for ( indexs[start] = 0; indexs[start] < array.length; indexs[start]++ )
{
recursionSub (list, count, array, 0, start, indexs);
if (start == count - 1)
{
String[] temp = new String[count];
for ( int i = count - 1; i >= 0; i-- )
{
temp[start - i] = array[indexs[start - i]];
}
if (!"0".equals (temp[0]))
{
list.add (temp);
}
}
}
return list;
}
}