티스토리 뷰


title: "최대공약수와 최소공배수(2609)"
category: 백준[Class-2]
tags: [C++, JavaScript, 백준]
date: "2021-03-19"


문제 링크

최대공약수와 최소공배수(2609)

C++

#include <iostream>
#include <vector>

using namespace std;

int getGCD(int a, int b){
    if(b==0)
        return a;
    else
        return getGCD(b, a%b);
}

// 문제 풀이 함수
void solution(){
    int a, b;
    cin >> a >> b;

    int gcdAB = getGCD(a, b);
    int lcmAB = a*b/gcdAB;

    cout<<gcdAB<<endl;
    cout<<lcmAB<<endl;
}

bool exists(const char* fileName){
    FILE* fp;
    if((fp = fopen(fileName, "r"))){
        fclose(fp);
        return true;
    }
    return false;
}

int main() {
    if(exists("stdin")){
        freopen("stdin", "r", stdin);
        solution();
        fclose(stdin);
    }
    else{
        solution();
    }

    return 0;
}

JavsScript

const fs = require("fs");
// split 조절
const input = fs.readFileSync("dev/stdin").toString().trim().split(" ");

// 문제 풀이
const n = +input[0];
const m = +input[1];

const getGCD = (a, b) => {
  if (b === 0) return a;
  else return getGCD(b, a % b);
};

const gcdAB = getGCD(n, m);
const lcmAB = (n * m) / gcdAB;

console.log(gcdAB);
console.log(lcmAB);
728x90
반응형

'Baekjoon Solutions > Class-2' 카테고리의 다른 글

[백준] 덩치(7568)  (0) 2021.03.21
[백준] 수 정렬하기 2(2751)  (0) 2021.03.19
[백준] 영화감독 숌(1436)  (0) 2021.03.19
[백준] 단어 정렬(1181)  (0) 2021.03.19
[백준] 체스판 다시 칠하기(1018)  (0) 2021.03.19
댓글
05-09 18:50
링크