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

21.02.10 - [BOJ] 2920. 음계 #10

Closed
suhyunsim opened this issue Feb 14, 2021 · 0 comments
Closed

21.02.10 - [BOJ] 2920. 음계 #10

suhyunsim opened this issue Feb 14, 2021 · 0 comments
Assignees
Labels
브론즈 BOJ - 브론즈 성공 맞은 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Feb 14, 2021

문제

핵심 아이디어

  • 증가하는 배열이면 ascending 감소 descending 아니면 mixed

어려운 점, 실수

  • 풀긴 풀었는데 코드가 좀 지저분하다.
  • 다른 풀이를 참고했다.
  • 시간이나 메모리는 동일!

풀이

  • 내가 푼 것
package main.java.com.poogle.BOJ.Q2920;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            list.add(sc.nextInt());
        }
        if (list.get(0) == 1) {
            for (int i = 0; i < 8; i++) {
                if (list.get(i) != i + 1) {
                    System.out.println("mixed");
                    return;
                }
            }
            System.out.println("ascending");
            return;
        }
        if (list.get(0) == 8) {
            for (int i = 0; i < 8; i++) {
                if (list.get(i) != 8 - i) {
                    System.out.println("mixed");
                    return;
                }
            }
            System.out.println("descending");
        } else System.out.println("mixed");
    }
}
  • 다른 풀이
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int[] data = new int[8];
		boolean ascending = true;
		boolean descending = true;
		
		for (int i = 0; i < 8; i++) {
			data[i] = sc.nextInt();
		}

		for (int i = 1; i < 8; i++) {
			if (data[i] > data[i - 1]) descending = false;
			if (data[i] < data[i - 1]) ascending = false;
		}
		
		if (ascending) System.out.println("ascending");
		else if (descending) System.out.println("descending");
		else System.out.println("mixed");
	}
}
@suhyunsim suhyunsim self-assigned this Feb 14, 2021
@suhyunsim suhyunsim added 브론즈 BOJ - 브론즈 구현 성공 맞은 문제 labels Feb 14, 2021
@suhyunsim suhyunsim added this to the 2월 2주 차 milestone Feb 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
브론즈 BOJ - 브론즈 성공 맞은 문제
Projects
None yet
Development

No branches or pull requests

1 participant