import java.util.*; public class MainExercises { // اصلاح متد برای ایجاد مثلث به صورت آرایه دو بعدی Jagged public char[][] generateTriangle(int n) { char[][] triangle = new char[n][]; for (int i = 0; i < n; i++) { triangle[i] = new char[i + 1]; for (int j = 0; j <= i; j++) { // شرط قرارگیری ستاره: ستون اول، ستون آخر یا ردیف آخر if (j == 0 || j == i || i == n - 1) { triangle[i][j] = '*'; } else { triangle[i][j] = ' '; } } } return triangle; } // متد پیمایش مارپیچی ماتریس public int[] spiralTraversal(int[][] matrix) { if (matrix == null || matrix.length == 0) return new int[0]; int rows = matrix.length; int cols = matrix[0].length; int[] result = new int[rows * cols]; int top = 0, bottom = rows - 1, left = 0, right = cols - 1; int index = 0; while (top <= bottom && left <= right) { for (int i = left; i <= right; i++) result[index++] = matrix[top][i]; top++; for (int i = top; i <= bottom; i++) result[index++] = matrix[i][right]; right--; if (top <= bottom) { for (int i = right; i >= left; i--) result[index++] = matrix[bottom][i]; bottom--; } if (left <= right) { for (int i = bottom; i >= top; i--) result[index++] = matrix[i][left]; left++; } } return result; } // متد افراز عدد (Integer Partitioning) public int[][] intPartitions(int n) { List> results = new ArrayList<>(); generatePartitions(n, n, new ArrayList<>(), results); int[][] res = new int[results.size()][]; for (int i = 0; i < results.size(); i++) { res[i] = results.get(i).stream().mapToInt(Integer::intValue).toArray(); } return res; } private void generatePartitions(int target, int max, List current, List> results) { if (target == 0) { results.add(new ArrayList<>(current)); return; } for (int i = Math.min(target, max); i >= 1; i--) { current.add(i); generatePartitions(target - i, i, current, results); current.remove(current.size() - 1); } } public static void main(String[] args) { // تست دستی در صورت نیاز } }