본문 바로가기
TIL

12.08 (TIL-코딩문제)

by 오랑이귀엽다 2023. 12. 8.

정수 제곱근 판별

 

나의 풀이

더보기

public class Solution {
    public long solution(long n) {
        long answer = 0;
        for(long i=1; i<=n; i++)
        {
            if(i*i==n)
            {
                answer= (i+1)*(i+1);
                break;
            }
            else if (i*i>=n)
            {
                break;
            }
        }
        if(answer==0)
        {
            answer =-1;
        }
        
        return answer;
    }
}

 

다른 사람 풀이

더보기
using System;
public class Solution {
    public long solution(long n) {
        long x = (long)Math.Sqrt(n);
            return (x*x == n) ? (x+1)*(x+1) : -1;
    }
}

 

정수 내림차순으로 배치하기

 

나의 풀이

더보기

public class Solution {
    public long solution(long n) {
        char[] arr = n.ToString().ToCharArray();
        Array.Sort(arr);
        Array.Reverse(arr);        
        long answer = Convert.ToInt64(new string(arr));
        
        return answer;
    }
}

 

다른사람 풀이

더보기
using System;

public class Solution {
    public long solution(long n) {
      long answer = 0;

        string nString = n.ToString();
        int[] answerList = new int[nString.Length];

        for (int i = 0; i < nString.Length; i++)
        {
            answerList[i] = int.Parse(nString[i].ToString());
        }

        Array.Sort(answerList);
        Array.Reverse(answerList);
        nString = "";

        for (int i = 0; i < answerList.Length; i++)
        {
            nString += answerList[i];
        }

        answer = long.Parse(nString);

        return answer;
    }
}

 

히샤드 수

 

나의 풀이

더보기

public class Solution {
         public  bool solution(int x)
        {
            int temp = x;
            int value = 0;
            bool answer = false;
            while (temp > 0)
            {
                value += temp % 10;
                temp = temp / 10;
            }

            if (x % value == 0)
                answer = true;

            return answer;
        }
}

 

다른사람 풀이

더보기
using System.Linq;

public class Solution {
    public bool solution(int x) {
        bool answer = true;

        var temp = x.ToString().ToList().Select(y => int.Parse(y.ToString())).Sum();

        if (x % temp != 0)
            answer = false;

        return answer;
    }
}

'TIL' 카테고리의 다른 글

12.12 (TIL-Unity)  (0) 2023.12.12
12.11 (TIL-Unity)  (0) 2023.12.11
12.07 (TIL-Unity)  (0) 2023.12.07
12.06 (TIL-코딩문제)  (0) 2023.12.06
12.05 (TIL-Unity)  (1) 2023.12.05