Programmers lv-0

μœ ν•œμ†Œμˆ˜ νŒλ³„ν•˜κΈ° - Java [ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ μž…λ¬Έ]

κ·€ν”Όν‚€ 2022. 11. 28. 15:41

 

❀️ Problem

더보기
  • 문제
    μ†Œμˆ˜μ  μ•„λž˜ μˆ«μžκ°€ κ³„μ†λ˜μ§€ μ•Šκ³  μœ ν•œκ°œμΈ μ†Œμˆ˜λ₯Ό μœ ν•œμ†Œμˆ˜λΌκ³  ν•©λ‹ˆλ‹€. λΆ„μˆ˜λ₯Ό μ†Œμˆ˜λ‘œ κ³ μΉ  λ•Œ μœ ν•œμ†Œμˆ˜λ‘œ λ‚˜νƒ€λ‚Ό 수 μžˆλŠ” λΆ„μˆ˜μΈμ§€ νŒλ³„ν•˜λ €κ³  ν•©λ‹ˆλ‹€. μœ ν•œμ†Œμˆ˜κ°€ 되기 μœ„ν•œ λΆ„μˆ˜μ˜ 쑰건은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

    1) κΈ°μ•½λΆ„μˆ˜λ‘œ λ‚˜νƒ€λ‚΄μ—ˆμ„ λ•Œ, λΆ„λͺ¨μ˜ μ†ŒμΈμˆ˜κ°€ 2와 5만 μ‘΄μž¬ν•΄μ•Ό ν•©λ‹ˆλ‹€.
    2) 두 μ •μˆ˜ a와 bκ°€ λ§€κ°œλ³€μˆ˜λ‘œ μ£Όμ–΄μ§ˆ λ•Œ, a/bκ°€ μœ ν•œμ†Œμˆ˜μ΄λ©΄ 1을, λ¬΄ν•œμ†Œμˆ˜λΌλ©΄ 2λ₯Ό returnν•˜λ„λ‘ solution ν•¨μˆ˜λ₯Ό μ™„μ„±ν•΄μ£Όμ„Έμš”.

 

  • μ œν•œ 사항
    • a, bλŠ” μ •μˆ˜
    • 0 < a ≤ 1,000
    • 0 < b ≤ 1,000

 

  • μž…μΆœλ ₯ 예 & μ„€λͺ…
no a b result
1 7 20 1
2 11 22 1
3 12 21 2
  1. λΆ„μˆ˜ 7/20은 κΈ°μ•½λΆ„μˆ˜ μž…λ‹ˆλ‹€. λΆ„λͺ¨ 20의 μ†ŒμΈμˆ˜κ°€ 2, 5 이기 λ•Œλ¬Έμ— μœ ν•œμ†Œμˆ˜μž…λ‹ˆλ‹€. λ”°λΌμ„œ 1을 returnν•©λ‹ˆλ‹€.
  2. λΆ„μˆ˜ 11/22λŠ” κΈ°μ•½λΆ„μˆ˜λ‘œ λ‚˜νƒ€λ‚΄λ©΄ 1/2 μž…λ‹ˆλ‹€. λΆ„λͺ¨ 2λŠ” μ†ŒμΈμˆ˜κ°€ 2 뿐이기 λ•Œλ¬Έμ— μœ ν•œμ†Œμˆ˜ μž…λ‹ˆλ‹€. λ”°λΌμ„œ 1을 returnν•©λ‹ˆλ‹€.
  3. λΆ„μˆ˜ 12/21λŠ” κΈ°μ•½λΆ„μˆ˜λ‘œ λ‚˜νƒ€λ‚΄λ©΄ 4/7 μž…λ‹ˆλ‹€. λΆ„λͺ¨ 7은 μ†ŒμΈμˆ˜κ°€ 7 μ΄λ―€λ‘œ λ¬΄ν•œμ†Œμˆ˜μž…λ‹ˆλ‹€. λ”°λΌμ„œ 2λ₯Ό returnν•©λ‹ˆλ‹€.

 


 

πŸ’› Solution

풀이

import java.util.ArrayList;
import java.util.List;
class Solution {
    public int solution(int a, int b) {
        int common = gcd(a, b);
        b = b/common;

        ArrayList<Integer> arr = new ArrayList<>();
        for(int i=1; i<=b; i++) {
            if(b%i==0) {
                arr.add(Integer.valueOf(i));
            }
        }

        ArrayList<Integer> check = new ArrayList<>();
        for(int i=0; i<arr.size(); i++) {
            if(arr.get(i)==1 || arr.get(i)%2==0 || arr.get(i)%5==0) {
                continue;
            } else {
                check.add(i);
            }
        }         

        if(check.isEmpty()) {
            return 1;
        } else {
            return 2;
        }

    }

    //μœ ν΄λ¦¬λ“œ ν˜Έμ œλ²• -> μ΅œλŒ€κ³΅μ•½μˆ˜ μΆ”μΆœ
    //= 각 λΆ„μˆ˜μ˜ λΆ„μžμ™€ λΆ„λͺ¨λ₯Ό μ΅œλŒ€κ³΅μ•½μˆ˜λ‘œ λ‚˜λˆ„μ–΄ κΈ°μ•½λΆ„μˆ˜λ‘œ λ§Œλ“ λ‹€
    public static int gcd(int num1, int num2) {

        int a = Math.max(num1, num2);
        int b = Math.min(num1, num2);
        int tmp = a;
        int c = 0;

        while (b!=0) {
            c = a%b;
            a = b;
            b = c;
        }

        return a;

    }

}

 

μ²˜λ¦¬μ†λ„ Very Good


 

πŸ’œ Comment

이 λ¬Έμ œμ—μ„œ κ°€μž₯ κ³ λ‚œμ„ κ²ͺ은 건 ArrayList.isEmpty()μ˜€λ‹€.

λ‚΄κ°€ μƒκ°ν•œ 그림은 인수 a와 b의 μ΅œλŒ€κ³΅μ•½μˆ˜λ₯Ό κ΅¬ν•΄μ„œ κΈ°μ•½λΆ„μˆ˜ν™” ν•œ λ‹€μŒμ— arr에 λΆ„λͺ¨(b)의 μ†ŒμΈμˆ˜λ₯Ό λ‹€ 넣어두고, arr[i]의 값이 1, 2 λ˜λŠ” 5일 λ•Œ arr.remove(i)λ₯Ό ν•œ 뒀에 arr.isEmpty() μ‹€ν–‰ 값이 trueλ©΄ μœ ν•œ μ†Œμˆ˜ / falseλ©΄ λ¬΄ν•œ μ†Œμˆ˜λ‘œ νŒλ³„λ‚˜λŠ” κ²ƒμ΄μ—ˆκ±΄λ§Œ...

 

import java.util.ArrayList;
import java.util.List;
class Solution {
    public int solution(int a, int b) {
        int common = gcd(a, b);
        b = b/common;

        ArrayList<Integer> arr = new ArrayList<>();
        for(int i=1; i<=b; i++) {
            if(b%i==0) {
                arr.add(Integer.valueOf(i));
            }
        }

        for(int i=0; i<arr.size(); i++) {
            if(arr.get(i)==1 || arr.get(i)%2==0 || arr.get(i)%5==0) {
                arr.remove(i);
            } else {
                continue;
            }
        }         

//        if(arr.size() == 0) {
        if(arr.isEmpty()) {
            return 2;
        } else {
            return 1;
        }

    }

    //μœ ν΄λ¦¬λ“œ ν˜Έμ œλ²• -> μ΅œλŒ€κ³΅μ•½μˆ˜ μΆ”μΆœ
    //= 각 λΆ„μˆ˜μ˜ λΆ„μžμ™€ λΆ„λͺ¨λ₯Ό μ΅œλŒ€κ³΅μ•½μˆ˜λ‘œ λ‚˜λˆ„μ–΄ κΈ°μ•½λΆ„μˆ˜λ‘œ λ§Œλ“ λ‹€
    public static int gcd(int num1, int num2) {

        int a = Math.max(num1, num2);
        int b = Math.min(num1, num2);
        int tmp = a;
        int c = 0;

        while (b!=0) {
            c = a%b;
            a = b;
            b = c;
        }

        return a;

    }

}

 

arr.size()==0 도 써보고 arr.isEmpty() 도 μ¨λ΄€μ§€λ§Œ κ³„μ†ν•΄μ„œ arrκ°€ λΉ„μ–΄μžˆμ§€ μ•Šλ‹€κ³  λ‚˜μ˜€λŠ” ν˜„μƒμ΄ λ°œμƒν–ˆλ‹€.

κ·Έλž˜μ„œ κ²°κ΅­ ArrayList 객체λ₯Ό 두 개 λ§Œλ“€κ²Œ λ˜μ—ˆλ‹€.. 이 뢀뢄에 λŒ€ν•΄μ„œλŠ” 좔가적인 곡뢀가 ν•„μš”ν•œ 것 κ°™λ‹€. 아직도 μ™œ μ΄λ ‡κ²Œ λ‚˜μ˜€λŠ”μ§€ 이해가 μ•ˆ κ°€λŠ” 쀑❓

 

7점!!!!!!!!! ν˜„μž¬ μˆœμœ„ 12931μœ„πŸ€©πŸ€©