結果

問題 No.14 最小公倍数ソート
ユーザー opqopq_opqopq_
提出日時 2015-05-14 10:13:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,572 ms / 5,000 ms
コード長 627 bytes
コンパイル時間 1,435 ms
コンパイル使用メモリ 39,124 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 07:40:51
合計ジャッジ時間 51,354 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 48 ms
4,380 KB
testcase_04 AC 4,572 ms
4,380 KB
testcase_05 AC 1,535 ms
4,376 KB
testcase_06 AC 1,791 ms
4,380 KB
testcase_07 AC 2,339 ms
4,380 KB
testcase_08 AC 3,077 ms
4,376 KB
testcase_09 AC 4,222 ms
4,376 KB
testcase_10 AC 4,095 ms
4,380 KB
testcase_11 AC 4,252 ms
4,376 KB
testcase_12 AC 4,368 ms
4,376 KB
testcase_13 AC 4,413 ms
4,376 KB
testcase_14 AC 4,297 ms
4,376 KB
testcase_15 AC 4,434 ms
4,376 KB
testcase_16 AC 1,647 ms
4,376 KB
testcase_17 AC 1,177 ms
4,380 KB
testcase_18 AC 523 ms
4,376 KB
testcase_19 AC 2,778 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <algorithm>
using namespace std;

#define N_MAX 10000
#define A_MAX 10000

int N;
int A[A_MAX];

int lcm(int a, int b) {
	return a * b / __gcd(a, b);
}

int main() {
	scanf("%d", &N);
	for (int i = 0; i < N; i++) scanf("%d", A + i);
	
	for (int i = 0; i + 2 < N; i++) {
		int mini = A_MAX * A_MAX + 1, id = i + 1;
		for (int j = i + 1; j < N; j++) {
			int x = lcm(A[i], A[j]);
			if (x <= mini) {
				if (x == mini && A[id] <= A[j]) continue;
				mini = x;
				id = j;
			}
		}
		swap(A[i + 1], A[id]);
	}
	
	for (int i = 0; i < N; i++) printf("%d%c", A[i], i + 1 < N ? ' ' : '\n');
	
	return 0;
}

0