ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JUNGOL...102
    일지 2021. 7. 26. 14:32

    Intermediate_Coder/분할정복/제곱근


    문제                                            

    임의의 정수 N이 주어졌을 때 N의 양의 제곱근의 정수부분을 출력하는 프로그램을 작성하라.

    양의 제곱근이란 다음을 만족하는 수 X 를 뜻한다. 

     

    N = X2 (X≥1)

     

    [ 주의  !!! ] 

    sqrt와 같은 함수를 사용하지 말아야 하며

    stdio.h 와 iostream 등 입출력 헤더에 있는 함수만이 사용가능하다. 

    이를 어길 경우 0점 처리한다.

     

    입력 형식                                     

    입력에는 263-1 이하의 양의 정수 N이 입력된다

     

    출력 형식                                     

    N의 제곱근의 정수부분을 출력한다.

     

    입력 예                                        

    8                | 16

     

    출력 예                                        

    2                | 4

     

    Hint!

    hancomc@hotmail.net


    SquareRoot.h

    #include <iostream>
    
    class SquareRoot : public Base
    {
    private:
    	long long GetSquareRoot(long long num);
    };

     

    SquareRoot.cpp

    void SquareRoot::Code()
    {
    	int num;
    
    	std::cin >> num;
    
    	std::cout << GetSquareRoot(num);
    }
    
    long long SquareRoot::GetSquareRoot(long long num)
    {
    	long long x = num;
    	for (int i = 0; i < 20; i++)
    	{
    		x = (x + num / x) / 2;
    	}
    	return x;
    }

     


    실행 결과 Accepted(50)

    원인 루트 값을 찾을 때 반복 횟수가 부족해서 발생하는 것으로 보인다.

    처리 방법 반복 회수를 늘린다.


     

    NadanKim/CodingTest_JUNGOL: JUNGOL 코딩 테스트를 위한 저장소 (github.com)

     

    NadanKim/CodingTest_JUNGOL

    JUNGOL 코딩 테스트를 위한 저장소. Contribute to NadanKim/CodingTest_JUNGOL development by creating an account on GitHub.

    github.com

     

    댓글

Designed by Tistory.