Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main
- {
- public static void main(String[] args)
- {
- int n = 4;
- int[][] pyramid = new int[][] {
- {7, 0, 0, 0},
- {5, 6, 0, 0},
- {6, 4, 3, 0},
- {4, 6, 5, 6}
- };
- int[][] temp = new int[n][n];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- temp[i][j] = 0;
- temp[0][0] = pyramid[0][0];
- for (int i = 1; i <= n - 1; i++)
- {
- temp[i][0] = pyramid[i][0] + temp[i - 1][0];
- }
- for (int i = 1; i <= n - 1; i++)
- {
- for (int j = 1; j <= n - 1; j++)
- {
- temp[i][j] = pyramid[i][j] + Math.max(temp[i - 1][j - 1], temp[i - 1][j]);
- }
- }
- System.out.println("Pyramid");
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- System.out.print(pyramid[i][j] + " ");
- }
- System.out.println("");
- }
- System.out.println("");
- System.out.println("Temp");
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- System.out.print(temp[i][j] + " ");
- }
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment