Tuesday, September 15, 2015

Rotating Array by a given angle(90, 180, 270, 360 degree)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.IOException;

import java.util.Scanner;

public class Main
{

 public static void main(String[] arg) throws IOException
 {

  int n;

  int[][] a;

  int r;

  Scanner sc = new Scanner(System.in);

  // 입력받는부분

  n = sc.nextInt();

  a = new int[n][n];

  for (int i = 0; i < n; i++)
  {
   for (int j = 0; j < n; j++)
    a[i][j] = sc.nextInt();
  }

  for (;;)
  {
   r = sc.nextInt();

   if (r == 0)
    break;

   // 여기서부터작성
   int[][] tempA = new int[n][n];
   
   int rowXDefault = 0;
   int rowYDefault = 0;
   int rowXIncrement = 0;
   int rowYIncrement = 0;
   int colXIncrement = 0;
   int colYIncrement = 0;
   
   if(r == 90){
    rowXDefault = 0;
    rowYDefault = n -1;
    rowXIncrement = -1;
    rowYIncrement = -1;
    colXIncrement = +1;
    colYIncrement = -1;
   }else if(r == 180){
    rowXDefault = n- 1;
    rowYDefault = n -1;
    rowXIncrement = -2;
    rowYIncrement = 0;
    colXIncrement = 0;
    colYIncrement = -2;
   }else if(r == 270){
    rowXDefault = n - 1;
    rowYDefault = 0;
    rowXIncrement = -1;
    rowYIncrement = +1;
    colXIncrement = -1;
    colYIncrement = -1;
   }else if(r == 360){
    //leave them
   }
   
   
   for (int i = 0; i < n; i++)
   {
    int indexX = rowXDefault;
    int indexY = rowYDefault;
    
    for (int j = 0; j < n; j++)
    {
     tempA[i + indexX][j + indexY] = a[i][j];
     
     indexX += colXIncrement;
     indexY += colYIncrement;
    
     
    }
    rowXDefault += rowXIncrement;
    rowYDefault += rowYIncrement;
   }
   
   a = tempA;
   
   // 출력하는부분
   for (int i = 0; i < n; i++)
   {
    for (int j = 0; j < n; j++)
    {
     System.out.print(a[i][j] + " ");

    }
    System.out.println();

   }
   
  }
  sc.close();
 }
 
 
}

No comments:

Post a Comment