結果

問題 No.14 最小公倍数ソート
ユーザー myantamyanta
提出日時 2017-05-02 10:15:48
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 717 bytes
コンパイル時間 414 ms
コンパイル使用メモリ 48,436 KB
実行使用メモリ 7,136 KB
最終ジャッジ日時 2023-10-12 05:05:53
合計ジャッジ時間 7,249 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
	vector<a_t> a;

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

		for(i=1;i<n;i++)
		{
			for(j=i;j<n;j++)
			{
				a[j].y=lcm(a[i].x, a[j].x);
			}
			sort(a.begin()+i, a.end(), cmp);
		}

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

	return 0;
}
0