MapleStory Finger Point

๐Ÿƒ‍โ™€๏ธprogrammers/Java

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - java ์กฐ๊ฑด์— ๋งž๊ฒŒ ์ˆ˜์—ด ๋ณ€ํ™˜ํ•˜๊ธฐ 2

HYEJU01 2024. 5. 2. 11:18

๐Ÿ’ก

50๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ง์ˆ˜  -> 2๋กœ ๋‚˜๋ˆ„๊ณ 

50๋ณด๋‹ค ์ž‘์€ ํ™€์ˆ˜ -> 2๋ฅผ ๊ณฑํ•˜๊ณ  +1 ์„ ํ•ด์ค€๋‹ค.

 

์ด ์ž‘์—…์„ x ๋ฒˆ ๋ฐ˜๋ณตํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐฐ์—ด arr(x) ๊ฐ€ ๋˜๋ฉด 

 arr(x) =  arr(x)  + 1 ์ด ๋˜๋Š” x ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ

์ด x ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.

----------------

 

ํ˜„์žฌ ๋ฐฐ์—ด๊ณผ ๊ทธ ์ „ ๋ฐฐ์—ด์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ฒŒ

๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•ด์„œ ๋น„๊ตํ•ด์ค˜์•ผํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  x ๋งŒํผ ๋ฐ˜๋ณตํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ while ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์ค€๋‹ค.

 

---------------

 

1) ์–•์€ ๋ณต์‚ฌ

์–•์€ ๋ณต์‚ฌ๋Š” ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ (๊ณต์œ ํ•จ) => ํ•จ๊ป˜ ๋ณ€๊ฒฝ

๊น‰์€ ๋ณต์‚ฌ๋Š” ๋…๋ฆฝ์ ์ธ ๊ฐ์ฒด๊ฐ€ ๋œ๋‹ค.

 

2) cnt ์‚ฌ์šฉ

if (arr[i] == a[i])

๋‘ ๋ฐฐ์—ด์„ ๋น„๊ตํ•ด์„œ ๊ฐ™์€ ๊ฐ’์ด ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ๋˜๋ฉด ๊ทธ ๊ฐ’์ด ๊ฐ™์€ ๊ฐ’์ด๋‹ค.

 

while ๋ฌธ์„ ๋‘ ๋ฐฐ์—ด์ด ๊ฐ™์„ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์ค˜์•ผํ•œ๋‹ค.

cnt (๊ฐ™์€ ๊ฐ’) ๋ฐฐ์—ด ๊ธธ์ด๋ž‘ ๊ฐ™์„ ๋•Œ ๊นŒ์ง€ ๋Œ๋ฆฐ๋‹ค.

 

3) count  = -1 

๊ฐ™์€ ๊ฐ’์„ ๋ฐœ๊ฒฌํ•˜๋ฉด ๊ทธ์ „ ์ธ๋ฑ์Šค ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฏ€๋กœ ๊ธฐ๋ณธ๊ฐ’์ด -1์ž„

 

 

 ๐Ÿ’ฌ arr.clone() :: ์–•์€ ๋ณต์‚ฌํ•˜๋Š” ๋ณต์ œ ๋ฉ”์„œ๋“œ, arr๊ณผ arrCopy๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฐ์—ด ๊ฐ์ฒด์ด์ง€๋งŒ, ๊ฐ ์š”์†Œ๋Š” ๋™์ผํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. ( ๋ณธ ๋ฐฐ์—ด๊ณผ ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์ด ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐ)

 

class Solution {
    public int solution(int[] arr) {
        
        int answer = -1;  // while ๋ฐ˜๋ณต ํšŸ์ˆ˜ ํ™•์ธ
        int[] a;
        int cnt = 0 ; 

        while(cnt != arr.length) {
            cnt = 0;
            a = arr.clone(); // ์–•์€ ๋ณต์‚ฌ  (while ๋Œ๋•Œ๋งˆ๋‹ค)
            
            for (int i = 0 ; i < arr.length; i++) {
                if (arr[i] < 50 && arr[i] % 2 != 0){
                    arr[i] = arr[i]*2 + 1;
                }else if  ( arr[i] >= 50 && arr[i] % 2 ==0){
                    arr[i] = arr[i]/2;
                } 
                if (arr[i] == a[i]) {
                    cnt ++; 
                }
            }   
            answer ++; 
        }
                    
        return answer;
    }
}

 


 

(์‹คํŒจ)   => ์ ‘๊ทผ์ด ์ž˜๋ชป๋๋‹ค ใ… 

์กฐ๊ฑด์— ๋งž์ง€ ์•Š์œผ๋ฉด ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ์ „ ๊ฐ’๊ณผ ๊ฐ™๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ณ  i-1 ์„ ์ถœ๋ ฅํ•œ๋‹ค.

import java.util.Arrays;

class Solution {
    public int solution(int[] arr) {
        int answer = 0; 
        boolean tf = false;
        int[] a = Arrays.copyOf(arr,arr.length);

        while(tf = false) {
            for (int i = 0 ; i < arr.length; i++) {
                if ( arr[i] >= 50 && arr[i] % 2 ==0){
                    a[i] = arr[i]/2;
                } else if (arr[i] < 50 && arr[i] % 2 != 0){
                    a[i] = arr[i]*2 + 1;
                }  else {
                    tf = true;
                    answer = i-1;
                    break;
                }
            }
        };
        
        return answer;
    }
}

(์‹คํŒจ) => ์ด๋ ‡๊ฒŒํ•˜๋ฉด ๊ฐ™์€ ๋ฐฐ์—ด๋ผ๋ฆฌ ๋น„๊ตํ•˜๋Š”๊ฑฐ๋ผ ์ƒˆ ๋ฐฐ์—ด์— ๋ฐฐ์—ด์„ ๋„ฃ์–ด์ฃผ๋ฉด์„œ ๋น„๊ตํ•ด์•ผํ•œ๋‹ค ใ…  

๊ทธ์ „ ๋ฐฐ์—ด๋ผ๋ฆฌ ๋น„๊ตํ•ด์„œ ์ฐพ์•„๋‚ธ๋‹ค.

import java.util.Arrays;

class Solution {
    public int solution(int[] arr) {
        int answer = 0; 
        boolean tf = false;
        int[] a = Arrays.copyOf(arr,arr.length);

        do {
            for (int i = 0 ; i < arr.length; i++) {
                if ( arr[i] >= 50 && arr[i] % 2 ==0){
                    a[i] = arr[i]/2;
                } else if (arr[i] < 50 && arr[i] % 2 != 0){
                    a[i] = arr[i]*2 + 1;
                }
                if (a[i] == a[i]){
                    tf = true;
                    answer = i - 1;
                    break;
                }
            }  
        }while (tf == true) ;
        
        return answer;
    }
}