結果

問題 No.14 最小公倍数ソート
ユーザー myantamyanta
提出日時 2017-05-02 10:27:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,648 ms / 5,000 ms
コード長 896 bytes
コンパイル時間 493 ms
コンパイル使用メモリ 45,284 KB
実行使用メモリ 4,368 KB
最終ジャッジ日時 2023-10-12 05:08:59
合計ジャッジ時間 52,024 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 49 ms
4,368 KB
testcase_04 AC 4,648 ms
4,356 KB
testcase_05 AC 1,562 ms
4,356 KB
testcase_06 AC 1,824 ms
4,356 KB
testcase_07 AC 2,379 ms
4,348 KB
testcase_08 AC 3,133 ms
4,368 KB
testcase_09 AC 4,249 ms
4,352 KB
testcase_10 AC 4,166 ms
4,356 KB
testcase_11 AC 4,313 ms
4,348 KB
testcase_12 AC 4,454 ms
4,356 KB
testcase_13 AC 4,504 ms
4,352 KB
testcase_14 AC 4,367 ms
4,352 KB
testcase_15 AC 4,515 ms
4,352 KB
testcase_16 AC 1,675 ms
4,348 KB
testcase_17 AC 1,201 ms
4,352 KB
testcase_18 AC 532 ms
4,352 KB
testcase_19 AC 2,824 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<vector>
#include<algorithm>


using namespace std;


struct a_t
{
	int x, y;
};


int gcd(int a, int b)
{
	int c;
	while((c=a%b))
	{
		a=b;
		b=c;
	}
	return b;
}


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


bool cmp(const a_t& lhs, const a_t& rhs)
{
	if(lhs.y!=rhs.y) return lhs.y<rhs.y;
	return lhs.x<rhs.x;
}


int main(void)
{
	int n, i, j, x;
	int min_lcm, min_idx;
	vector<int> a;

	while(scanf("%d", &n)==1)
	{
		a.resize(n);
		for(i=0;i<n;i++)
		{
			scanf("%d", &a[i]);
		}

		for(i=0;i<n;i++)
		{
			min_lcm=100000000+1;
			min_idx=i+1;
			for(j=i+1;j<n;j++)
			{
				x=lcm(a[i], a[j]);
				if(min_lcm>x)
				{
					min_lcm=x;
					min_idx=j;
				}
				else if(min_lcm==x && a[min_idx]>a[j])
				{
					min_idx=j;
				}
			}
			swap(a[i+1], a[min_idx]);
		}

		for(i=0;i<n-1;i++)
		{
			printf("%d ", a[i]);
		}
		printf("%d\n", a[i]);
	}

	return 0;
}
0