結果

問題 No.14 最小公倍数ソート
ユーザー myantamyanta
提出日時 2017-05-02 10:26:16
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 890 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 45,096 KB
実行使用メモリ 4,476 KB
最終ジャッジ日時 2023-10-12 05:07:23
合計ジャッジ時間 52,058 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 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<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=10000;
			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