Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CCC 2018 J4/S2 - Sunflowers
- public class Sunflower {
- public static int[][] rotate(int[][] grid, int deg, int num) { // num: length of the 2d array
- int[][] result = new int[num][num];
- for (int i = 0; i < num; i++) {
- for (int j = 0; j < num; j++) {
- if (deg == 90) {
- result[i][j] = grid[num - j - 1][i];
- } else if (deg == 180) {
- result[i][j] = grid[num - i - 1][num - j - 1];
- } else if (deg == 270) {
- result[i][j] = grid[j][num - i - 1];
- }
- }
- }
- return result;
- }
- public static void main(String[] args) {
- int[][] arr = {{4, 3, 1}, {6, 5, 2}, {9, 7 ,3}}; // change the array as you like to test it out
- // remember to make sure that it has a valid answer though!
- int min = arr[0][0];
- int deg = 0;
- if (min > arr[0][arr.length - 1]) {
- min = arr[0][arr.length - 1];
- deg = 270;
- }
- if (min > arr[arr.length - 1][arr.length - 1]) {
- min = arr[arr.length - 1][arr.length - 1];
- deg = 180;
- }
- if (min > arr[arr.length - 1][0]) {
- min = arr[arr.length - 1][0];
- deg = 90;
- }
- int[][] result;
- result = rotate(arr, deg, arr.length);
- for (int[] row : result) {
- for (int n : row) {
- System.out.print(n + " ");
- }
- System.out.println();
- }
- }
- }
Add Comment
Please, Sign In to add comment