結果

問題 No.14 最小公倍数ソート
ユーザー FF256grhyFF256grhy
提出日時 2015-06-01 00:59:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,513 ms / 5,000 ms
コード長 764 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 26,300 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 18:12:41
合計ジャッジ時間 50,094 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 48 ms
4,380 KB
testcase_04 AC 4,513 ms
4,380 KB
testcase_05 AC 1,512 ms
4,376 KB
testcase_06 AC 1,766 ms
4,376 KB
testcase_07 AC 2,306 ms
4,376 KB
testcase_08 AC 3,044 ms
4,380 KB
testcase_09 AC 4,111 ms
4,376 KB
testcase_10 AC 4,044 ms
4,376 KB
testcase_11 AC 4,188 ms
4,376 KB
testcase_12 AC 4,314 ms
4,380 KB
testcase_13 AC 4,368 ms
4,376 KB
testcase_14 AC 4,241 ms
4,380 KB
testcase_15 AC 4,386 ms
4,380 KB
testcase_16 AC 1,627 ms
4,380 KB
testcase_17 AC 1,163 ms
4,376 KB
testcase_18 AC 517 ms
4,380 KB
testcase_19 AC 2,748 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//とりあえず愚直な解法を投げてみる

#include <stdio.h>

int gcd(int, int);
int lcm(int, int);

int n, a[10000];

int main(void) {
	scanf("%d", &n);
	int i, j;
	for(i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	
	printf("%d ", a[0]);
	for(i = 0; i < n - 1; i++) {
		int min = 100000000, p = -1;
		for(j = i + 1; j < n; j++) {
			int l = lcm(a[i], a[j]);
			if( l < min || (l == min && a[j] < a[p]) ) {
				min = l;
				p = j;
			}
		}
		int temp = a[i + 1];
		a[i + 1] = a[p];
		a[p] = temp;
		printf("%d ", a[i + 1]);
	}
	printf("\n");
	
	return 0;
}

int gcd(int x, int y) {
	int max = (y < x) ? x : y;
	int min = (x < y) ? x : y;
	if(min == 0) { return max; }
	return gcd(min, max % min);
}

int lcm(int x, int y) {
	return x * y / gcd(x, y);
}
0