Second homework #4
@@ -1,3 +1,7 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MainExercises
|
||||
{
|
||||
/*
|
||||
@@ -18,10 +22,37 @@ public class MainExercises
|
||||
|
||||
the output has to be a two-dimensional array of characters, so don't just print the triangle!
|
||||
*/
|
||||
|
||||
public char[][] generateTriangle(int n) {
|
||||
|
||||
// todo
|
||||
return null;
|
||||
if (n <= 0)
|
||||
return new char[0][0];
|
||||
|
||||
char[][] triangle = new char[n][n];
|
||||
for (int i = 0 ; i< n ; i++)
|
||||
{
|
||||
if (i == n-1)
|
||||
{
|
||||
for (int j = 0 ; j < n ; j++)
|
||||
{
|
||||
triangle[i][j] ='*';
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for (int j = 0 ; j <= i ; j++) {
|
||||
if (j == 0 || j== i)
|
||||
triangle[i][j] = '*';
|
||||
|
||||
else
|
||||
triangle[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return triangle;
|
||||
|
||||
}
|
||||
|
||||
@@ -59,7 +90,46 @@ public class MainExercises
|
||||
*/
|
||||
public int[] spiralTraversal(int[][] matrix) {
|
||||
// todo
|
||||
return null;
|
||||
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
int rows = matrix.length;
|
||||
int cols = matrix[0].length;
|
||||
int[] spiral = new int[rows * cols];
|
||||
|
||||
int top = 0;
|
||||
int bottom = rows - 1;
|
||||
int left = 0;
|
||||
int right = cols - 1;
|
||||
int index = 0;
|
||||
|
||||
while (top <= bottom && left <= right) {
|
||||
for (int j = left; j <= right; j++) {
|
||||
spiral[index++] = matrix[top][j];
|
||||
}
|
||||
top++;
|
||||
|
||||
for (int i = top; i <= bottom; i++) {
|
||||
spiral[index++] = matrix[i][right];
|
||||
}
|
||||
right--;
|
||||
|
||||
if (top <= bottom) {
|
||||
for (int j = right; j >= left; j--) {
|
||||
spiral[index++] = matrix[bottom][j];
|
||||
}
|
||||
bottom--;
|
||||
}
|
||||
|
||||
if (left <= right) {
|
||||
for (int i = bottom; i >= top; i--) {
|
||||
spiral[index++] = matrix[i][left];
|
||||
}
|
||||
left++;
|
||||
}
|
||||
}
|
||||
return spiral;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -92,12 +162,76 @@ public class MainExercises
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
if (n <= 0) {
|
||||
return new int[0][0];
|
||||
}
|
||||
|
||||
List<int[]> partitionsList = new ArrayList<>();
|
||||
generatePartitions(n, n, new ArrayList<>(), partitionsList);
|
||||
|
||||
int[][] result = new int[partitionsList.size()][];
|
||||
for (int i = 0; i < partitionsList.size(); i++) {
|
||||
result[i] = partitionsList.get(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void generatePartitions(int remaining, int maxValue, List<Integer> current, List<int[]> result) {
|
||||
if (remaining == 0) {
|
||||
int[] partition = new int[current.size()];
|
||||
for (int i = 0; i < current.size(); i++) {
|
||||
partition[i] = current.get(i);
|
||||
}
|
||||
result.add(partition);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = Math.min(maxValue, remaining); i >= 1; i--) {
|
||||
current.add(i);
|
||||
generatePartitions(remaining - i, i, current, result);
|
||||
current.remove(current.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main()
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner in = new Scanner(System.in);
|
||||
MainExercises me = new MainExercises();
|
||||
|
||||
System.out.print("Enter a number: ");
|
||||
int n = in.nextInt();
|
||||
|
||||
|
||||
char[][] triangle = me.generateTriangle(n);
|
||||
for (char[] row : triangle) {
|
||||
for (char c : row) {
|
||||
System.out.print(c);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
int[][] matrix = {
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9}
|
||||
};
|
||||
int[] spiral = me.spiralTraversal(matrix);
|
||||
for (int num : spiral) {
|
||||
System.out.print(num + " ");
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
int[][] partitions = me.intPartitions(n);
|
||||
for (int[] p : partitions) {
|
||||
for (int i = 0; i < p.length; i++) {
|
||||
System.out.print(p[i]);
|
||||
if (i < p.length - 1) System.out.print(", ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user