티스토리 뷰

Algorithm/Baekjoon Online Judge

3036번 '링'

할루루 2018. 8. 10. 13:03
import java.io.*;
import java.util.*;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(in.readLine());
String[] input = in.readLine().split(" ");
final int base = Integer.parseInt(input[0]);
for(int i=1; i<count; ++i) {
final int radius = Integer.parseInt(input[i]);
int tempBase = base;
int tempRadius = radius;
while(tempRadius != 0) {
int temp = tempBase;
tempBase = tempRadius;
tempRadius = temp % tempRadius;
}
System.out.println(base / tempBase + "/" + radius / tempBase);
}
}
}


최대공약수의 활용.




let count = Int(readLine()!)!

let input = readLine()!.split(separator: " ").map { Int($0)! }

let base = input[0]

for i in 1..<count {

    let radius = input[i]

    var tempBase = base

    var tempRadius = radius

    while tempRadius != 0 {

        let temp = tempBase

        tempBase = tempRadius

        tempRadius = temp % tempRadius

    }

    print("\(base / tempBase)/\(radius / tempBase)")

}



'Algorithm > Baekjoon Online Judge' 카테고리의 다른 글

2902번 'KMP는 왜 KMP일까?'  (0) 2018.08.10
10451번 '순열 사이클'  (0) 2018.08.10
1850번 '최대공약수'  (0) 2018.08.10
13241번 '최소공배수'  (0) 2018.08.10
1181번 '단어 정렬'  (0) 2018.08.10
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함