0

我在网上发现了一个问题:给定输入大小,打印所有该大小的格式良好的数字。

示例:size = 3 数字:123、234、125 等条件,比如数字是 abc,然后 a < b < c

我正在尝试为此编写一个递归代码,并且由于我很擅长递归,因此无法弄清楚基本情况,或者如何摆脱递归。我有一个想法:

  1. 我从给定大小的最小格式良好的数字开始(只需用 填充数组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);
    }
4

3 回答 3

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]
于 2012-04-22T11:55:40.527 回答
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"]
于 2012-04-22T15:18:06.323 回答
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);
    }
}

请注意,如果这是家庭作业,您还没有完成。

于 2012-04-21T19:57:17.683 回答