SQL과 알고리즘 둘다 연습 문제 푸는데, 프로그래머스 사이트 자체적으로 문제 현황이 기록되는줄 이제 알았다. 모든 문제를 기록하고 있었는데 반복되는 경우도 많아서 SQL은 특별하게 어려웠거나 특징적인 것들을 기록하고자 하고,
Java알고리즘은 매번 새롭다보니 계속해서 이 방식으로 후기글을 작성하고자 한다. 이번엔 Math클래스의 Math.sqrt()라는 메서드를 사용해보았다. 루트를 연산해주는 프로그램인데 어딘가 쓸대가 있을것 같은데 확 와닿지는 않는다. 근대 방법 자체가 재밌어서 푸는데 재미있었다.
0. 프로그래머스
class Solution {
static double rowN;
static int resultN;
int answer;
public int solution(int n) {
if(n<1 || n>1000000){
System.out.println("잘못된 숫자 입력");
}else{
rowN = Math.sqrt(n);
resultN = (int)rowN;
if(rowN == resultN){
answer = 1;
}else{
answer = 2;
}
}
return answer;
}
}
Java
복사
검색해보니 진짜 문제풀이만을 위한 아래같은 코드도 있었다. 그런데 제한사항 같은 유효성체크는 안하는건가?? 그냥 답만 빠르게 내면 되는 것인가 혼란스럽다. 뭔가 프로그램같지 않고 암기식 공부처럼 흘러가는데 어디에 기준을 두어야 될지 모르겠다..
class Solution {
public int solution(int n) {
return (int)Math.sqrt(n)*(int)Math.sqrt(n) == n ? 1:2;
}
}
Java
복사
1. Main.java
이번 실습까지는 나누어서 진행해보고 다음 실습부터 하나로 합쳐야 겠다. 별것 아니긴 한데 객체를 생성하고 메서드를 불러오는 것을 익숙하게 하기 위해서 일부러 했던 과정인데
package Algorithm12;
public class Main {
public static void main(String[] args){
Solution solution = new Solution();
solution.input();
}
}
Java
복사
2. Solution.java
연산은 약간 고등학교 수학정도로 생각해야 했다. 근대 오래되서 기억이 잘 나질 않는다. 루트는 많이 들어봤지만 제곱수라고 명칭이 있었는지는 기억이 안났다. 아무튼 제곱근을 구하는 것이기 때문에 분명 Math클래스에서 기능이 있을 것이라 생각해서 뒤져보았는데 Math.root가 없는것이다. 그런데 잘 보니 sqrt라고 줄임말로 써져있는 것 같았다. 4로 테스트를 해보니까 역시 제곱근을 구하는 메서드가 맞아서 사용하게 되었다. 유효성체크를 모두 걸어주고 테스트를 진행했다.
package Algorithm12;
import java.util.Scanner;
public class Solution {
/*문제 설명
어떤 자연수를 제곱했을 때 나오는 정수를
제곱수라고 합니다.
n^2 = result => int jegopsu
정수 n이 매개변수로 주어질 때,
if result == jegopsu =>return 1
else =2
n이 제곱수라면 1을
아니라면 2를 return하도록 solution 함수를 완성해주세요.
제한사항
1 ≤ n ≤ 1,000,000*/
int inputNum;
double rootRow;
int rootResult;
int resultFlag;
int tryNum;
Scanner sc = new Scanner(System.in);
public Solution() {
}
void input() {
while (true) {
System.out.println("아무 숫자를 입력해 보세요. 제곱수 판별기입니다.");
inputNum = sc.nextInt();
if (inputNum < 1 || inputNum > 1000000) {
System.out.println("숫자는 1~100만까지만 입력하세요.");
continue;
}
calculate(inputNum);
break;
}
}
void calculate(int inputNum) {
// 만약 4를 입력하면 2가 제곱수
// input 4 jegopsu 2
// 16을 입력하면 4가 제곱수.
// 16 = 4*4 : input = je*je
rootRow = Math.sqrt(inputNum);
rootResult = (int) rootRow;
System.out.println(rootRow);
System.out.println(rootResult);
if (rootRow == rootResult) {
resultFlag = 1;
outputRoot(inputNum, rootRow, rootResult, resultFlag);
} else {
resultFlag = 2;
outputRoot(inputNum, rootRow, rootResult, resultFlag);
}
}
void outputRoot(int inputNum, double rootRow, int rootResult, int resultFlag) {
if (resultFlag == 1) {
System.out.printf("입력하신 %d는 제곱수로, 제곱근은 %d입니다. -%d", inputNum, rootResult, resultFlag);
} else {
System.out.printf("입력하신 %d는 제곱수가 아닙니다. -%d\n", inputNum, resultFlag);
}
tryAgain();
}
void tryAgain() {
System.out.println("다시 실행하시겠습니까?\n[1] YES [2] NO");
tryNum = sc.nextInt();
while (true) {
if (tryNum == 1) {
input();
break;
} else if (tryNum == 2) {
System.out.println("안녕히가세요.");
System.exit(0);
break;
}
}
}
}
Java
복사