main
[백준-3036번/Java] 링 본문
https://www.acmicpc.net/problem/3036
* 최대공약수 이용 (유클리드 호제법)
import java.io.*;
import java.util.*;
public class Main {
public static class SimpleFraction {
int numerator;
int denominator;
public SimpleFraction(int numerator, int denominator) {
int fraction_gcd = gcd(numerator, denominator);
this.numerator = numerator / fraction_gcd;
this.denominator = denominator / fraction_gcd;
}
@Override
public String toString() {
return this.numerator + "/" + this.denominator;
}
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
StringTokenizer stk = new StringTokenizer(br.readLine(), " ");
int[] arr = new int[N];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(stk.nextToken());
}
for (int i = 1; i < arr.length; i++) {
System.out.println(new SimpleFraction(arr[0], arr[i]));
}
}
public static int gcd(int a, int b) {
int temp;
if (a < b) {
temp = a;
a = b;
b = temp;
}
int r = a % b;
if (r == 0) {
return b;
}
return gcd(b, r);
}
}
728x90
'Algorithm' 카테고리의 다른 글
[백준-2407번/Java] 조합 (0) | 2022.11.26 |
---|---|
[Array-10/Java] 봉우리 (0) | 2022.11.25 |
[백준-1002번/Java] 터렛 (0) | 2022.11.22 |
[백준-3053번/Java] 택시 기하학 (0) | 2022.11.22 |
[백준-2477번/Java] 참외밭 (0) | 2022.11.22 |
Comments