티스토리 뷰

문제풀이/백준

2659. 십자카드 문제

BiteSnail 2024. 1. 3. 13:29

문제 개요

원형으로 연속인 4개의 숫자가 주어졌을 때 시작위치를 변경해서 구할 수 있는 가장 작은 수인 시계수를 구하고 이 시계수가 모든 시계수 중 몇 번째로 작은 수인지 구하는 문제입니다.

문제 접근

가장 작은 시계수를 구하는 방법

가능한 조합의 수는 총 4개 밖에 없으므로 반복문을 통해 주어진 숫자들에서 가장 작은 시계수를 구할 수 있습니다.

몇 번째로 작은 시계수인지 확인하는 방법

처음에는 적절한 수식으로 나타내는 것이 맞다고 생각했는데, 마땅히 생각나지 않아서 해당 숫자가 시계수인지 여부를 확인하는 배열을 만들어서 사용해야 될 것 같습니다.

주어진 수가 시계수인지 확인하려면 다음 조건을 만족하면 됩니다.
4자리 수 x에 대해서

  1. 자리를 옮겼을 때 더 작지 않을 것
  2. 자리를 옮겼을 때 1000보다 작아지지 않을 것

코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    static BufferedReader bf;
    static boolean[] isNotClockNum;
    static int[] cards;

    static void input() throws IOException {
        cards = Arrays.stream(bf.readLine().split(" "))
                        .mapToInt(Integer::parseInt)
                        .toArray();
    }

    static int nextClockNum(int clockNum){
        return (clockNum % 1000) * 10 + (clockNum / 1000);
    }

    static void process(){
        int clockNum = cards[0] * 1000 + cards[1] * 100 + cards[2] * 10 + cards[3];
        int nextNum = clockNum;

        // 주어진 카드에서 시계수를 구하는 부분
        for(int i=0;i<3;i++){
            nextNum = nextClockNum(nextNum);
            clockNum = Math.min(clockNum, nextNum);
        }

        // 몇 번째 시계수인지 구하는 부분
        int result = 1;
        for(int i=1111;i<clockNum;i++){
            if(isNotClockNum[i]){
                continue;
            }
            result++;
            int current = i;
            // 가장 작은 수를 제외하고는 시계수에서 제외
            for(int c=0;c<3;c++){
                current = nextClockNum(current);
                isNotClockNum[current] = true;
                // 자리수 중에 0이 포함되면 시계수에서 제외
                if(current < 1000){
                    result--;
                    break;
                }
            }
        }

        System.out.println(result);
    }

    public static void main(String[] args) throws IOException {
        bf = new BufferedReader(new InputStreamReader(System.in));
        isNotClockNum = new boolean[10000];
        input();
        process();
    }
}

문제

https://www.acmicpc.net/problem/2659

'문제풀이 > 백준' 카테고리의 다른 글

14501. 퇴사  (0) 2024.01.09
2668. 숫자고르기  (0) 2024.01.07
1244. 스위치 켜고 끄기  (0) 2024.01.02
2302. 극장 좌석  (0) 2024.01.01
9205. 맥주 마시면서 걸어가기  (1) 2023.12.31
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/11   »
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
글 보관함