fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int[][] gearWheels = new int[4][8];
  7. int[] dirFlag = new int[gearWheels.length];
  8.  
  9. for(int i=0; i<4; i++){
  10. String[] tmp = sc.nextLine().split("");
  11. for(int j=1; j<=8; j++){
  12. gearWheels[i][j-1] = Integer.parseInt(tmp[j]);
  13. }
  14. }
  15.  
  16. int cnt = Integer.parseInt(sc.nextLine());
  17.  
  18. for(int i=0; i<cnt; i++){
  19. String[] line = sc.nextLine().split(" ");
  20. int gearNum = Integer.parseInt(line[0])-1;
  21. int dir = Integer.parseInt(line[1]);
  22. int dirTmp = dir;
  23. dirFlag[gearNum] = dirTmp;
  24. for(int j=gearNum; j<gearWheels.length-1; j++){
  25. if(gearWheels[j][2]!=gearWheels[j+1][6]){
  26. dirTmp = dirTmp*(-1);
  27. }else{
  28. dirTmp = 0;
  29. }
  30. dirFlag[j+1] = dirTmp;
  31. }
  32.  
  33. dirTmp = dir;
  34. for(int j=gearNum; j>0; j--){
  35. if(gearWheels[j][6]!=gearWheels[j-1][2]){
  36. dirTmp = dirTmp*(-1);
  37. }else{
  38. dirTmp = 0;
  39. }
  40. dirFlag[j-1] = dirTmp;
  41. }
  42. rotateGearWheels(gearWheels, dirFlag);
  43. }
  44.  
  45. // 결과 출력
  46. System.out.println(gearWheels[0][0]*1+gearWheels[1][0]*2
  47. +gearWheels[2][0]*4+gearWheels[3][0]*8);
  48. sc.close();
  49. }
  50.  
  51. // 톱니바퀴 회전 시키는 메소드
  52. public static void rotateGearWheels(int[][] gearWheels, int[] dirFlag){
  53. for(int i=0; i<gearWheels.length; i++){
  54. if(dirFlag[i]==0){
  55. continue;
  56. }else if(dirFlag[i]==1){ // 시계 방향 회전
  57. int tmp = gearWheels[i][gearWheels[i].length-1];
  58. for(int j=gearWheels[i].length-1; j>0; j--){
  59. gearWheels[i][j] = gearWheels[i][j-1];
  60. }
  61. gearWheels[i][0] = tmp;
  62. }else if(dirFlag[i]==-1){ // 반시계 방향 회전
  63. int tmp = gearWheels[i][0];
  64. for(int j=0; j<gearWheels[i].length-1; j++){
  65. gearWheels[i][j] = gearWheels[i][j+1];
  66. }
  67. gearWheels[i][gearWheels[i].length-1] = tmp;
  68. }
  69. }
  70. }
  71.  
  72. }
Runtime error #stdin #stdout #stderr 0.11s 2249728KB
stdin
10101111
01111101
11001110
00000010
2
3 -1
1 1
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
	at Main.main(Main.java:12)