結果

問題 No.751 Frac #2
ユーザー Mcpu3
提出日時 2018-08-30 02:19:56
言語 Java
(openjdk 23)
結果
AC  
実行時間 159 ms / 1,000 ms
コード長 918 bytes
コンパイル時間 2,862 ms
コンパイル使用メモリ 79,388 KB
実行使用メモリ 55,220 KB
最終ジャッジ日時 2024-10-12 10:59:41
合計ジャッジ時間 10,235 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

class Main
{
	public static long gcd(long x, long y)
	{
		long r;
		x = Math.abs(x);
		y = Math.abs(y);
		if (y > x) {
			r = x;
			x = y;
			y = r;
		}
		while (y > 0) {
			r = x % y;
			x = y;
			y = r;
		}
		return x;
	}
	
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		long n1, n2, a, b, numerator = 0, denominator = 1, i;
		n1 = sc.nextInt();
		for (i = 0; i < n1; i++) {
			a = sc.nextInt();
			if (1 > i) numerator = a;
			else denominator *= a;
		}
		n2 = sc.nextInt();
		for (i = 0; i < n2; i++) {
			b = sc.nextInt();
			if (i % 2 == 1) numerator *= b;
			else denominator *= b;
		}
		sc.close();
		if (0 > denominator) {
			numerator = -numerator;
			denominator = -denominator;
		}
		System.out.print(numerator / gcd(numerator, denominator) + " " + denominator / gcd(numerator, denominator));
	}
}
0