結果

問題 No.14 最小公倍数ソート
ユーザー NotationNapNotationNap
提出日時 2015-04-23 07:43:06
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 634 bytes
コンパイル時間 2,219 ms
コンパイル使用メモリ 146,724 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-18 08:32:52
合計ジャッジ時間 49,170 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

typedef long long Int;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);++(i))

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}
int lcm(int a, int b) {
	return a / gcd(a, b) * b;
}

int a[10000];
int main() {
	int N;
	cin >> N;
	REP(i, N) cin >> a[i];
	sort(a + 1, a + N);
	REP(i, N-1) {
		int k = i + 1;
		int m = lcm(a[i], a[i + 1]);
		for (int j = i + 2; j < N; j++) {
			int m1 = lcm(a[i], a[j]);
			if (m > m1) {
				m = m1;
				k = j;
			}
		}
		if (i + 1 != k)
			swap(a[i + 1], a[k]);
	}
	cout << a[0];
	for (int i = 1; i < N; i++) cout << " " << a[i];
	cout << endl;

}
0