我在网上发现了一个问题:给定输入尺寸打印所有格式正确的数字。
示例:size = 3 数字:123,234,125等条件,说数字是abc然后a< b< ç
我正在尝试为此编写一个递归代码,因为我在递归时很糟糕,我无法弄清楚基本情况,或者如何摆脱递归。我有一个想法:
for loop
填充数组)。假设size = 3,我从123
开始。然后我继续arr[0] == (10 - size))
,因为给定大小的arr[0]
的最大值是一个格式良好的数字。 我的功能是printNumbers(int arr[], int size)
但我不确定这是否有效。需要一些关于正确方向的指示。
public void findNumbers(int arr[], int size, int pos)
{
if(arr[0] == (10 - size))
return;
if(arr[pos] == (10 - size + pos))
{
pos--;
findNumbers(arr,size,pos);
}
System.out.println(Arrays.toString(arr));
arr[pos] = arr[pos] + 1;
findNumbers(arr,size,pos);
}
public static void main(String[] args)
{
int size = 3;
int pos = size-1;
int arr[] = new int[size];
for(int i = 0; i<size; i++)
{
arr[i] = i+1;
}
//System.out.println(Arrays.toString(arr));
WellFormed obj = new WellFormed();
obj.findNumbers(arr, size, pos);
}
答案 0 :(得分:1)
import java.util.Arrays;
public class WellFormed {
public static int maxDigit;
public void findNumbers(int[] digits, int start, int currPos) {
if (currPos >= digits.length) {
System.out.println(Arrays.toString(digits));
return;
}
int maxDigitInCurrPos = maxDigit - digits.length + currPos + 1;
for (int i = start; i <= maxDigitInCurrPos; i++) {
digits[currPos] = i;
findNumbers(digits, i+1, currPos + 1);
}
}
public static void main(String[] args)
{
WellFormed obj = new WellFormed();
maxDigit = 5;
int inputSize = 3;
int[] digits = new int[inputSize];
obj.findNumbers(digits, 1, 0);
}
}
这很好用:
For inputSize = 3 and maxDigit = 5, output is:
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 3, 4]
[1, 3, 5]
[1, 4, 5]
[2, 3, 4]
[2, 3, 5]
[2, 4, 5]
[3, 4, 5]
答案 1 :(得分:1)
它必须是Java吗?这是五行Haskell的解决方案。我喜欢这个(以及其他Haskell代码),它基本上就像是对问题的定义。
wellFormed::Int->[a]->[[a]]
wellFormed _ [] = []
wellFormed 1 xs = map (\x -> [x]) xs
wellFormed n (x:xs) = helper n x xs ++ wellFormed n xs
where helper n init rest = map ((:) init) (wellFormed (n - 1) rest)
> wellFormed 3 "123456789"
["123","124","125","126","127","128","129","134","135","136","137","138","139","145","146","147","148","149","156","157","158","159","167","168","169","178","179","189","234","235","236","237","238","239","245","246","247","248","249","256","257","258","259","267","268","269","278","279","289","345","346","347","348","349","356","357","358","359","367","368","369","378","379","389","456","457","458","459","467","468","469","478","479","489","567","568","569","578","579","589","678","679","689","789"]
答案 2 :(得分:0)
我认为你有更多的障碍,而不是没有问题的概念,看到你的清晰描述。所以我曾经回答过这种典型的“家庭作业”。
public void giveWellFormedNumbers(int inputSize) {
int[] digits = new int[intputSize];
giveWFNumbers(digits, 0, 1);
}
/**
* @param fromIndex the numbers of digits done, the starting index to continue.
*/
private giveWFNumbers(int[] digits, int fromIndex, int fromDigitValue) {
if (fromIndex >= digits.length) {
System.out.println(Arrays.toString(digits));
return;
}
// Do one digit yourself, at digits[fromIndex]:
int maxDigit = 10 - digits.length; // What is the maximal digit you have to do?
for (int digitValue = fromDigitValue; digitValue <= maxDigit; ++digitValue) {
digits[fromIndex = digitValue;
giveFWNumbers(digits, fromIndex + 1, digitValue + 1);
}
}
介意,如果这是作业,你还没有完成。