結果

問題 No.14 最小公倍数ソート
ユーザー opqopq_
提出日時 2015-05-14 10:13:29
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 4,063 ms / 5,000 ms
コード長 627 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 34,264 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-06 03:35:08
合計ジャッジ時間 45,005 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:16:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   16 |         scanf("%d", &N);
      |         ~~~~~^~~~~~~~~~
main.cpp:17:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   17 |         for (int i = 0; i < N; i++) scanf("%d", A + i);
      |                                     ~~~~~^~~~~~~~~~~~~

ソースコード

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