삐까냥의 파도타기
14891번) 톱니바퀴 본문
문제 출처 : https://www.acmicpc.net/problem/14891
자잘한 실수 발생;
이상하게 자바가 버벅거리고 넘나 느려서 미치겄네요.
2018년 4월 7일 - 다듬지 않은 코드 (소요시간 1시간 8분) public class Q14891 { static int[][] gear = new int[4][8]; public static void main(String[] args) { Scanner sc = new Scanner(System.in);
for (int i = 0; i < 4; i++) { String temp = sc.nextLine();
int[] tempGear = new int[8]; for (int j = 0; j < 8; j++) { char tempChar = temp.charAt(j); if (tempChar == '1') { tempGear[j] = 1; } } gear[i] = tempGear; // showGear(i); } // System.out.println();
int rotationNum = sc.nextInt(); for (int i = 0; i < rotationNum; i++) { int[] directions = new int[4];
int gearNum = (sc.nextInt()-1); int direction = sc.nextInt();
switch (gearNum) { case 0: directions[0] = direction; if (isRotation(0, 1)) { directions[1] = direction * -1; if (isRotation(1, 2)) { directions[2] = direction; if (isRotation(2, 3)) { directions[3] = direction * -1; } } } break;
case 1: directions[1] = direction; if (isRotation(0, 1)) { directions[0] = direction * -1; } if (isRotation(1, 2)) { directions[2] = direction * -1; if (isRotation(2, 3)) { directions[3] = direction; } } break;
case 2: directions[2] = direction; if (isRotation(1, 2)) { directions[1] = direction * -1; if (isRotation(0, 1)) { directions[0] = direction; } } if (isRotation(2, 3)) { directions[3] = direction * -1; } break;
case 3: directions[3] = direction; if (isRotation(2, 3)) { directions[2] = direction * -1; if (isRotation(1, 2)) { directions[1] = direction; if (isRotation(0, 1)) { directions[0] = direction * -1; } } }
break; } for (int j = 0; j < 4; j++) { leftAndRightRotation(j, directions[j]); // showGear(j); } // System.out.println(); }
int result = 0; if (gear[0][0] == 1) { result += 1; } if (gear[1][0] == 1) { result += 2; } if (gear[2][0] == 1) { result += 4; } if (gear[3][0] == 1) { result += 8; } System.out.println(result); }
static boolean isRotation(int leftGearNum, int rightGearNum) { if (gear[leftGearNum][2] != gear[rightGearNum][6]) { return true; } return false; }
static void showGear(int gearNum) { for (int i = 0; i < 8; i++) { char temp = 'S'; if (gear[gearNum][i] == 0) { temp = 'N'; } System.out.print(temp + " "); } System.out.println(); }
static void leftAndRightRotation(int gearNum, int direction) { if (direction == 0) { return; } int[] tempGear = gear[gearNum]; if (direction == -1) { //left int tempValue = tempGear[0]; for (int i = 0; i < 7; i++) { tempGear[i] = tempGear[i+1]; } tempGear[7] = tempValue; } else { //right int tempValue = tempGear[7]; for (int i = 7; i > 0; i--) { tempGear[i] = tempGear[i-1]; } tempGear[0] = tempValue; } gear[gearNum] = tempGear; } } |
2018년 4월 7일 - 다듬은 코드 public class Q14891 { static int[][] gear = new int[4][8]; public static void main(String[] args) { Scanner sc = new Scanner(System.in);
for (int i = 0; i < 4; i++) { String temp = sc.nextLine();
int[] tempGear = new int[8]; for (int j = 0; j < 8; j++) { char tempChar = temp.charAt(j); if (tempChar == '1') { tempGear[j] = 1; } } gear[i] = tempGear; }
int rotationNum = sc.nextInt(); for (int i = 0; i < rotationNum; i++) { //기준 기어 정보 받기 int gearNum = (sc.nextInt()-1); int direction = sc.nextInt();
//기어의 회전 방향 정하기 int[] directions = getDirections(gearNum, direction);
//정해진 회전 방향으로 기어 회전하기 for (int j = 0; j < 4; j++) { doRotation(j, directions[j]); } }
//결과 보여주기 showResult(); }
static int[] getDirections(int gearNum, int direction) { int[] directions = new int[4];
switch (gearNum) { case 0: directions[0] = direction; if (isRotation(0, 1)) { directions[1] = direction * -1; if (isRotation(1, 2)) { directions[2] = direction; if (isRotation(2, 3)) { directions[3] = direction * -1; } } } break;
case 1: if (isRotation(0, 1)) { directions[0] = direction * -1; } directions[1] = direction; if (isRotation(1, 2)) { directions[2] = direction * -1; if (isRotation(2, 3)) { directions[3] = direction; } } break;
case 2: if (isRotation(1, 2)) { directions[1] = direction * -1; if (isRotation(0, 1)) { directions[0] = direction; } } directions[2] = direction; if (isRotation(2, 3)) { directions[3] = direction * -1; } break;
case 3: if (isRotation(2, 3)) { directions[2] = direction * -1; if (isRotation(1, 2)) { directions[1] = direction; if (isRotation(0, 1)) { directions[0] = direction * -1; } } } directions[3] = direction; break;
default : break; } return directions; }
static boolean isRotation(int leftGearNum, int rightGearNum) { if (gear[leftGearNum][2] != gear[rightGearNum][6]) { return true; } return false; }
static void doRotation(int gearNum, int direction) { if (direction == 0) { //회전 안함. return; } int[] tempGear = gear[gearNum]; if (direction == -1) { //left 회전 int tempValue = tempGear[0]; for (int i = 0; i < 7; i++) { tempGear[i] = tempGear[i+1]; } tempGear[7] = tempValue; } else { //right 회전 int tempValue = tempGear[7]; for (int i = 7; i > 0; i--) { tempGear[i] = tempGear[i-1]; } tempGear[0] = tempValue; } gear[gearNum] = tempGear; }
static void showResult() { int result = 0; if (gear[0][0] == 1) { result += 1; } if (gear[1][0] == 1) { result += 2; } if (gear[2][0] == 1) { result += 4; } if (gear[3][0] == 1) { result += 8; } System.out.println(result); } } |
2018년 4월 12일 - 다듬지 않은 코드 (소요시간 39분) import java.util.Scanner; public class Q14891 { static int[][] gears = new int[4][8]; public static void main(String[] args) { Scanner sc = new Scanner(System.in);
for (int i = 0 ; i < 4 ; i++) { String temp = sc.next(); for (int j = 0 ; j < 8; j++) { char tempChar = temp.charAt(j); if (tempChar == '1') { gears[i][j] = 1; } } }
int rotationNum = sc.nextInt(); for (int i = 0 ; i < rotationNum; i++) { rotationGear(sc.nextInt()-1, sc.nextInt()); }
showResult(); }
static void rotationGear(int gearNum, int direction) { int[] isRotation = new int[4]; isRotation[gearNum] = direction;
switch (gearNum) { case 0 : if (gears[0][2] != gears[1][6]) { isRotation[1] = -direction; if (gears[1][2] != gears[2][6]) { isRotation[2] = direction; if (gears[2][2] != gears[3][6]) { isRotation[3] = -direction; } } } break;
case 1 : if (gears[0][2] != gears[1][6]) { isRotation[0] = direction*-1; } if (gears[1][2] != gears[2][6]) { isRotation[2] = direction*-1; if (gears[2][2] != gears[3][6]) { isRotation[3] = direction; } } break;
case 2 : if (gears[1][2] != gears[2][6]) { isRotation[1] = direction*-1; if (gears[0][2] != gears[1][6]) { isRotation[0] = direction; } } if (gears[2][2] != gears[3][6]) { isRotation[3] = direction*-1; } break;
case 3 : if (gears[2][2] != gears[3][6]) { isRotation[2] = direction*-1; if (gears[1][2] != gears[2][6]) { isRotation[1] = direction; if (gears[0][2] != gears[1][6]) { isRotation[0] = direction*-1; } } } break; }
for (int i = 0 ; i < 4; i++) { direction = isRotation[i]; // System.out.println(direction); if (direction != 0) { setGear(i, direction); } } // showGear(); }
static void showGear() { System.out.println(); for (int i = 0 ; i < 4; i++) { for (int j = 0 ; j < 8; j++) { System.out.print(gears[i][j]); } System.out.println(); } System.out.println(); }
static void setGear(int gearNum, int direction) { if (direction == 1) { int tempValue = gears[gearNum][7]; for (int i = 7 ; i > 0; i--) { gears[gearNum][i] = gears[gearNum][i-1]; } gears[gearNum][0] = tempValue; } else { int tempValue = gears[gearNum][0]; for (int i = 0 ; i < 7; i++) { gears[gearNum][i] = gears[gearNum][i+1]; } gears[gearNum][7] = tempValue; } }
static void showResult() { int result = 0; if (gears[0][0] == 1) { result += 1; } if (gears[1][0] == 1) { result += 2; } if (gears[2][0] == 1) { result += 4; } if (gears[3][0] == 1) { result += 8; }
System.out.println(result); } } |
'코딩 > 삼성 SW 역량 테스트 기출' 카테고리의 다른 글
14499번) 주사위 굴리기 (0) | 2018.04.08 |
---|---|
14501번) 퇴사 (0) | 2018.04.07 |
14888번) 연산자 끼워넣기 (0) | 2018.04.06 |
14503번) 로봇 청소기 (0) | 2018.04.06 |
14889번) 스타트와 링크 (0) | 2018.04.06 |