编程题:37
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
static List results = new ArrayList<>();
static int count = 0;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int N = scanner.nextInt();
    decompose(N, N, 1, new ArrayList<>());
    for (int i = 0; i < results.size(); i++) {
        System.out.print(results.get(i));
        if ((i + 1) % 4 == 0 || i == results.size() - 1) {
            System.out.println();
        } else {
            System.out.print(";");
        }
    }
}

public static void decompose(int n, int original, int start, List<Integer> path) {
    if (n == 0) {
        StringBuilder sb = new StringBuilder(original + "=");
        for (int i = 0; i < path.size(); i++) {
            if (i > 0) {
                sb.append("+");
            }
            sb.append(path.get(i));
        }
        results.add(sb.toString());
        return;
    }
    for (int i = start; i <= n; i++) {
        path.add(i);
        decompose(n - i, original, i, path);
        path.remove(path.size() - 1);
    }
}

}

posted on 2025-06-03 22:42  Swishy  阅读(6)  评论(0)    收藏  举报