Algorithm/Solving

[책-알고리즘퍼즐68]12.제곱근의 숫자

비상펭귄 2020. 2. 6. 17:25
728x90

문제

 제곱근을 소수로 나타내었을 때 0~9의 모든 숫자가 가장 빨리 나타나는 최소 정수를 구하시오. 단, 여기서는 양의 제곱근만을 대상으로 합니다. 정수 부분을 포함하는 경우와 소수 부분만 취하는 경우 각각에 대해 모두 구하시오.

 

난이도

IQ 85

 

목표시간

20분

 

풀이(c#,VS)

1. 

using System;
using System.Linq;

namespace _12_SquareRoot
{
    class Program
    {
        static void Main(string[] args)
        {

            //정수포함           
            int targetNum = 2;
            while (true)
            {
                string sTargetNum = Math.Sqrt(targetNum).ToString().Replace(".", "");
                Console.Write(targetNum + "/" + Math.Sqrt(targetNum) + "/" + sTargetNum);
                if (StringDuplicationDelete(sTargetNum).Length == 10)
                {
                    Console.WriteLine(targetNum);
                    break;
                }
                targetNum++;
            }

            //소수포함           
            //int targetNum = 2;
            //while (true)
            //{
            //    string sTargetNum = string.Empty;
            //    try
            //    {
            //        sTargetNum = Math.Sqrt(targetNum).ToString().Split('.')[1];
            //        Console.Write(targetNum +"/" + Math.Sqrt(targetNum) + "/"+ sTargetNum);
            //    }
            //    catch
            //    {
            //        //split 에러
            //    }

            //    if (StringDuplicationDelete(sTargetNum).Length == 10)
            //    {
            //        Console.WriteLine(targetNum);
            //        break;
            //    }
            //    targetNum++;
            //}
        }

        public static string StringDuplicationDelete(string target)
        {
            string newString = string.Empty;

            for (int i=0; i< target.Length; i++)
            {
                if (!newString.Contains(target[i]))
                {
                    newString += target[i];
                }
               
            }

            Console.WriteLine(" /  " + newString);
            return newString;
        }
    }
}

 

문제정답(사용언어:python)

 

1.

from math import sqrt

# 정수 부분을 포함하는 경우
i =1
while True:
     i +=1
     # 소수점을 제거하고 왼쪽 10문자 추출
     string = '{:10.10f}'.format(sqrt(i)).replace('.','')[0:10]
     # 중복을 제거해서 10문자라면 종료
     if len(set(string)) == 10 :   
         break

print(i)

# 소수 부분만 계산하는 경우
i =1
while True:
     i +=1
     # 소수점을 분할하여 소수 부분만을 취득
     string = '{:10.10f}'.format(sqrt(i)).split('.')[1]
     # 중복을 제거해서 10문자라면 종료
     if len(set(string)) == 10 :
         break

print(i)

 

-

 

 

728x90