結果

問題 No.14 最小公倍数ソート
ユーザー masamasa
提出日時 2015-01-22 05:17:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,674 ms / 5,000 ms
コード長 733 bytes
コンパイル時間 653 ms
コンパイル使用メモリ 63,932 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 03:28:24
合計ジャッジ時間 52,307 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 49 ms
4,384 KB
testcase_04 AC 4,674 ms
4,380 KB
testcase_05 AC 1,570 ms
4,376 KB
testcase_06 AC 1,835 ms
4,380 KB
testcase_07 AC 2,395 ms
4,376 KB
testcase_08 AC 3,151 ms
4,380 KB
testcase_09 AC 4,258 ms
4,376 KB
testcase_10 AC 4,183 ms
4,380 KB
testcase_11 AC 4,338 ms
4,376 KB
testcase_12 AC 4,465 ms
4,380 KB
testcase_13 AC 4,514 ms
4,376 KB
testcase_14 AC 4,399 ms
4,380 KB
testcase_15 AC 4,536 ms
4,376 KB
testcase_16 AC 1,685 ms
4,380 KB
testcase_17 AC 1,204 ms
4,384 KB
testcase_18 AC 537 ms
4,380 KB
testcase_19 AC 2,845 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <climits>

using namespace std;

int gcd(int a, int b) {
	if (b == 0) {
		return a;
	}
	return gcd(b, a % b);
}

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

int main() {
	int n;

	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	for (int i = 0; i < n - 1; i++) {
		int pos = i + 1;
		int mini = INT_MAX;
		for (int j = i + 1; j < n; j++) {
			int tmp = lcm(a[i], a[j]);
			if (tmp < mini || (tmp == mini && a[j] < a[pos]) ) {
				mini = tmp;
				pos = j;
			}
		}
		swap(a[i+1], a[pos]);
	}

	for (int i = 0; i < n; i++) {
		printf("%d%c", a[i], i != n - 1 ? ' ' : '\n');
	}
	return 0;
}
0