1) SW Expert Academy 정책상 문제 자체를 퍼가는 것은 금지되며 링크와 출처로 명시해 주시기 바랍니다.
2) 문제에 대한 본인의 풀이에 대해서는 개인 학습 등 상업적 용도가 아닌 경우에만 문제 출처와 함께 게시가 가능합니다.
※ 저작권 이슈가 있을 시 법적 제재를 받을 수 있으니 참고하여주시기 바랍니다.
문제 설명
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
나의 풀이
import java.util.Scanner;
class Solution {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
int N = sc.nextInt(); // 16
int M = sc.nextInt(); // 80
// 2차원 배열 입력
int[][] arr = new int[N][M];
for (int i = 0; i < N; i++) {
String line = sc.next(); // 입력은 공백 없이 문자열로 주어짐
for (int j = 0; j < M; j++) {
arr[i][j] = line.charAt(j) - '0';
}
}
int[][] Pattern = {
{0, 0, 0, 1, 1, 0, 1}, // 0
{0, 0, 1, 1, 0, 0, 1}, // 1
{0, 0, 1, 0, 0, 1, 1}, // 2
{0, 1, 1, 1, 1, 0, 1}, // 3
{0, 1, 0, 0, 0, 1, 1}, // 4
{0, 1, 1, 0, 0, 0, 1}, // 5
{0, 1, 0, 1, 1, 1, 1}, // 6
{0, 1, 1, 1, 0, 1, 1}, // 7
{0, 1, 1, 0, 1, 1, 1}, // 8
{0, 0, 0, 1, 0, 1, 1} // 9
};
int answer = 0;
// 암호코드 발견했는지 여부
outer: for (int i = 0; i < N; i++) {
for (int j = M - 1; j >= 0; j--) {
if (arr[i][j] == 1) { // 1을 만나는 순간 암호코드 끝부분이다
int[] code = new int[8]; // 암호는 8개의 숫자
int idx = 7; // 코드 뒤에서부터 채운다
int pos = j;
while (idx >= 0) {
// 7자리 패턴을 찾아야 함
for (int k = 0; k < 10; k++) { // 0~9 패턴 중 하나 찾기
boolean match = true;
for (int m = 0; m < 7; m++) {
if (pos - 6 + m < 0 || arr[i][pos - 6 + m] != Pattern[k][m]) {
match = false;
break;
}
}
if (match) {
code[idx] = k;
idx--;
pos -= 7;
break; // 패턴 찾았으면 다음 숫자 찾으러
}
}
}
// 암호코드 검증
int sum = 0;
for (int c = 0; c < 8; c++) {
if (c % 2 == 0) sum += code[c] * 3; // 홀수자리
else sum += code[c]; // 짝수자리
}
if (sum % 10 == 0) { // 정상적인 코드
for (int c = 0; c < 8; c++) {
answer += code[c];
}
}
break outer; // 한 줄만 해독하면 됨
}
}
}
System.out.println("#" + test_case + " " + answer);
}
}
}
다른 풀이
import java.util.*;
class Solution{
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
HashMap<String, Integer> map = new HashMap<>();
String[] nums = {"0001101", "0011001", "0010011","0111101","0100011","0110001","0101111","0111011","0110111","0001011"};
for(int i = 0; i < nums.length; i++){
map.put(nums[i],i);
}
for(int tc = 1; tc <= T; tc++) {
int n = sc.nextInt();
int m = sc.nextInt();
String[] info = new String[n];
for(int i = 0; i < n; i++){
info[i] = sc.next();
}
int[] answer = new int[8];
int count = 7;
int even = 0;
int odd = 0;
for(int i = 0; i < n; i++){
if(!info[i].contains("1")) continue;
int idx = info[i].lastIndexOf("1") + 1;
while(count != -1){
answer[count] = map.get(info[i].substring(idx - 7,idx));
if(count % 2 == 0){
odd += answer[count];
}else{
even += answer[count];
}
count--;
idx -= 7;
}
break;
}
int result = odd + even;
if((odd * 3 + even) % 10 != 0){
result = 0;
}
System.out.println("#" + tc + " " + result);
}
}
}
'👨💻 Coding Test > SWEA' 카테고리의 다른 글
[SW Expert Academy/Java/D3] 1244.최대 상금 (0) | 2025.05.07 |
---|---|
[SW Expert Academy/Java/D3] 1234.비밀번호 (0) | 2025.04.26 |
[SW Expert Academy/Java/D3] 1230.암호문3 (0) | 2025.04.24 |
[SW Expert Academy/Java/D3] 1229.암호문2 (0) | 2025.04.23 |
[SW Expert Academy/Java/D3] 1228.암호문1 (0) | 2025.04.22 |