public static void main(String[] args) { /*Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt();*/ int n=8; int m=3; int res = 0; for(int i=1; i<=m; i++){ res = res + dfs(i, n, 0, m, 0, 0); } System.out.println(res); } /** * @param i 当前val * @param n 总val * @param now 当前总val * @param m 最多m * @param res 结果 * @param pre 前一步的步数 * @return */ public static int dfs(int i, int n, int now, int m, int res, int pre){ now += i; if(now == n){ return ++res; }else if (now > n){ return res; } for(int j=1; j<=m; j++){ if(j != i && j!=pre){ res = dfs(j, n, now, m, res, i); } } return res; }
请问有a的java答案嘛?写了个递归,但是只有0.4
全部评论
(2) 回帖