问一道程序题(C或者JAVA)编写?

输入n(1 < n < 5)个m(0<m<100), 按顺序打印它们组成的所有数字
(如果输入非法, 提示用户重新输入)
截图要求提供输入非法m值的运行结果(字符/超范围数字)
样例输入: 5 20 3
样例输出: 2035 2053 3205 3520 5203 5320

这个可以写成递归也可以写成非递归,递归写起来简单,就用递归的给你例子:

代码如下:

public class SimpleCodeTest {

public static void DFS(List<Integer> candidate, String prefix, int showLength) {

if (prefix.length() != 0 && prefix.length() == showLength) {

System.out.println(prefix);

}

for (int i = 0; i < candidate.size(); i++) {

List<Integer> temp = new LinkedList<Integer>(candidate);

int item = (int) temp.remove(i); // 取出被删除的元素,这个元素当做一个组合用掉了

DFS(temp, prefix + item, showLength);

}

}

public static void main(String[] args) {

Integer[] array = { 1, 2, 0, 9 };

List<Integer> list = Arrays.asList(array);

DFS(list, "", list.size());

}

}

运行图如下:

温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜