问题描述
用 java 实现的简单思路是什么呢?? 问题补充:好像意思没表达清除,Input: int[m]output: int[n]下边 code 里边半天没能看出input,解答一下吧。谢谢阿programming 写道
解决方案
/** * @author Tony.Yan */public class Test { private int m; private int[] set; private boolean first; private int position; public Test(int n, int m) { this.m = m; first = true; position = n - 1; set = new int[n]; for (int i = 0; i < n; i++) set[i] = i + 1; } public boolean hasNext() { return set[0] < m - set.length + 1; } public int[] next() { if (first) { first = false; return set; } if (set[set.length - 1] == m) position--; else position = set.length - 1; set[position]++; for (int i = position + 1; i < set.length; i++) set[i] = set[i - 1] + 1; return set; } public static void main(String[] args) { Test nOfm = new Test(3, 5); while (nOfm.hasNext()) { int[] set = nOfm.next(); for (int i = 0; i < set.length; i++) { System.out.print(set[i]); } System.out.println(); } }}
解决方案二:
代码的意思是从1到m个数找出n个数,set[]不就是你要输出的结果吗
解决方案三:
public Test(int n, int m) { this.m = m; first = true; position = n - 1; set = new int[n]; for (int i = 0; i < n; i++) set[i] = i + 1; } 这个地方就是input
解决方案四:
其实我建议楼上给个伪代码。留点思考空间
解决方案五:
使用递归简单