티스토리 뷰


title: "정수 제곱근 판별"
category: 프로그래머스[Level-1]
tags: [C++, JavaScript, 프로그래머스]
date: "2021-01-20"


문제 링크

정수 제곱근 판별

C++

#include <string>
#include <vector>
#include <cmath>

using namespace std;

long long solution(long long n) {
    long long answer = 0;

    if((long long)sqrt(n) == sqrt(n)) {
        answer = pow(sqrt(n)+1, 2);
    }
    else {
        answer = -1;
    }

    return answer;
}

JavaScript

function solution(n) {
  var answer = 0;

  if (Math.sqrt(n) === parseInt(Math.sqrt(n))) {
    answer = Math.pow(Math.sqrt(n) + 1, 2);
  } else {
    answer = -1;
  }

  return answer;
}
728x90
반응형
댓글
05-21 12:58
링크