結果

問題 No.14 最小公倍数ソート
ユーザー rpy3cpprpy3cpp
提出日時 2016-02-14 21:01:52
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 919 bytes
コンパイル時間 1,143 ms
コンパイル使用メモリ 65,480 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 05:20:50
合計ジャッジ時間 48,072 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 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 <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

int lcm(int a, int b){
    int ab = a * b;
    int tmp = 0;
    while (b){
        tmp = a % b;
        a = b;
        b = tmp;
    }
    return ab / a;
}

void lcm_sort(vector<int> & a){
    sort(a.begin() + 1, a.end());
    for (int i = 0; i < a.size() - 2; ++i){
        int ai = a[i];
        int min_lcm = 100000001;
        int k = 0;
        for (int j = i + 1; j != a.size(); ++j){
            int aj = a[j];
            int lcmij = lcm(ai, aj);
            if (lcmij < min_lcm){
                min_lcm = lcmij;
                k = j;
            } 
        }
        swap(a[i + 1], a[k]);
    }
}

int main()
{
    int n = 0;
    cin >> n;
    vector<int> a(n, 0);
    for (auto & ai : a) cin >> ai;
    lcm_sort(a);
    for (int i = 0; i != n; ++i) cout << a[i] << ((i == n-1) ? "\n" : " ");
    return 0;
}
0