結果

問題 No.14 最小公倍数ソート
ユーザー scachescache
提出日時 2014-11-12 03:34:53
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,423 bytes
コンパイル時間 3,294 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 61,408 KB
最終ジャッジ日時 2023-08-30 03:15:14
合計ジャッジ時間 10,814 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
60,700 KB
testcase_01 AC 136 ms
55,920 KB
testcase_02 AC 139 ms
55,772 KB
testcase_03 AC 552 ms
61,408 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Main p = new Main();
	}

	public Main() {
		Scanner sc = new Scanner(System.in);
		int[] a = new int[sc.nextInt()];
		for(int i=0;i<a.length;i++)
			a[i] = sc.nextInt();
		solve(a);
	}

	public void solve(int[] a) {
		LcmNumber[] lcms = new LcmNumber[a.length];
		for(int i=0;i<a.length;i++)
			lcms[i] = new LcmNumber(lcm(a[0], a[i]), a[i]);
		Arrays.sort(lcms, Math.min(1, lcms.length-1), lcms.length);	
		
		for(int i=1;i<a.length;i++){
			for(int j=i+1;j<a.length;j++){
				lcms[j].lcm = lcm(Math.max(lcms[i].num, lcms[j].num), Math.min(lcms[i].num, lcms[j].num));
			}
			Arrays.sort(lcms, i+1, lcms.length);	
		}
		
		for(int i=0;i<lcms.length-1;i++)
			System.out.print(lcms[i].num+ " ");
		System.out.println(lcms[lcms.length-1].num);
	}
	
	private class LcmNumber implements Comparable<LcmNumber>{
		int lcm;
		int num;
		
		public LcmNumber(int lcm, int num){
			this.lcm =lcm;
			this.num = num;
		}

		@Override
		public int compareTo(LcmNumber o) {
			if(this.lcm!=o.lcm)
				return this.lcm-o.lcm;
			else
				return this.num - o.num;
		}
	}
	
	private int gcm(int a, int b){
		if(a%b==0)
			return b;
		else
			return gcm(b, a%b);
	}
	
	private int lcm(int a, int b){
		return a*b/(gcm(a, b));
	}
}
0