Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] BOJ - 1244. 스위키 켜고 끄기 #47

Merged
merged 3 commits into from
Aug 3, 2023
Merged

[java] BOJ - 1244. 스위키 켜고 끄기 #47

merged 3 commits into from
Aug 3, 2023

Conversation

3o14
Copy link
Member

@3o14 3o14 commented Aug 2, 2023

📣 Related Issue

close #

🌁 Background

백준 문제 풀이

👩‍💻 Contents

BOJ 1244. 스위치 켜고 끄기
image

코드

1. 입력받기

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int n = Integer.parseInt(st.nextToken());
arr = new int[n];

st = new StringTokenizer(br.readLine());
// 스위치 상태 입력받기
for(int i=0; i<n; i++) {
    arr[i] = Integer.parseInt(st.nextToken());
}

2. 학생들 수만큼 반복수행

int m = Integer.parseInt(br.readLine());

int []data = new int[2];

for(int i=0; i<m; i++) {
    st = new StringTokenizer(br.readLine());
    for(int j=0; j<2; j++) {
        data[j] = Integer.parseInt(st.nextToken());
    }
    
    if(data[0] == 1) { // 남자일 경우
        arr = boy(data[1]);
    } else { // 여자일 경우
        //
        arr = girl(data[1]);
    }
}

3. 남학생 함수

public static int[] boy(int data) {
    for (int i = 0; i < arr.length; i++) {
        if ((i + 1) % data == 0) {
            arr[i] = arr[i] == 0 ? 1 : 0;
        }
    }
    return arr;
}

4. 여학생 함수

public static int[] girl(int data) {
    data--;
    arr[data] = arr[data] == 1 ? 0 : 1;
    for(int i=1; i<=data; i++) {
        if(data+i >= arr.length || data-i < 0) {
            break;
        }
        if(arr[data+i] == arr[data-i]) {
            arr[data+i] = arr[data+i] == 1 ? 0 : 1;
            arr[data-i] = arr[data-i] == 1 ? 0 : 1;
        } else {
            return arr;
        }
    }
    return arr;
}

5. 출력

*출력은 20글자 마다 줄바꿈을 한다.

int cnt = 1;
// 출력
for(int i=0; i<arr.length; i++) {
    if(i == 20*cnt) {
        System.out.println();
        cnt++;
    }
    System.out.print(arr[i] +" ");
}

📱 Screenshot

image

📝 Review Note

첫 PR이라 이렇게 하는게 맞는지 모르겠네요..! 어제 데일리 과제로 나왔던 문제 올려봅니다!
자바로 알고리즘을 풀기 시작한지 얼마 안돼서 브론즈 실버부터 차근차근 풀어나가겠습니다 😀

@3o14 3o14 requested a review from a team as a code owner August 2, 2023 02:03
@3o14 3o14 requested review from kgh2120 and Sumin-Kim-dev and removed request for a team August 2, 2023 02:03
@minjae9610
Copy link
Member

와 자세히 작성해주셨네요 ㄷㄷ
제대로 작성해주신 것 같습니다! 👍

gumi_4_wonjulee/boj/README.md Show resolved Hide resolved
gumi_4_wonjulee/boj/README.md Outdated Show resolved Hide resolved
Copy link
Member

@kgh2120 kgh2120 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!

gumi_4_wonjulee/boj/README.md Show resolved Hide resolved
20 배수 단위로 출력

Co-authored-by: 김수민(Sumin Kim) <[email protected]>
@3o14
Copy link
Member Author

3o14 commented Aug 3, 2023

  • 스위치 토글 값 변경 arr[i] ^= 1
  • boy함수 탐색시 data의 배수만 순회
  • 출력은 StringBuilder 사용

@3o14 3o14 merged commit 60ec84b into main Aug 3, 2023
@3o14 3o14 deleted the algo/3o14/boj1244 branch August 3, 2023 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants