本帖最后由 燃烧端午 于 2012-9-26 01:21 编辑
- public class Jafafenjie {
- public static void main(String[] args) {
- int res[] = new int[10];
- f1(20, 20, 1, res, 0);
- }
- public static void f1(int N, int sum, int cur, int res[], int depth) {
- if (sum <= 0) {
- if (sum == 0) {
- for (int i = 0; i < depth; i++) {
- System.out.print("+"+res[i]);
- }
- System.out.print("=20");
- System.out.println();
- }
- } else {
- if (cur < N) {
- res[depth] = cur;
- f1(N, sum - cur, cur + 1, res, depth + 1);
- f1(N, sum, cur + 1, res, depth);
- }
- }
- }
- }
复制代码 在网上看了一个C语言做的 我给改成java了希望能给楼主帮助啊……呵呵 |