結果

問題 No.14 最小公倍数ソート
ユーザー ytftytft
提出日時 2021-03-03 07:13:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 963 bytes
コンパイル時間 1,579 ms
コンパイル使用メモリ 166,308 KB
実行使用メモリ 10,268 KB
最終ジャッジ日時 2024-04-14 05:50:11
合計ジャッジ時間 8,611 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 70 ms
6,944 KB
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:38:118: warning: 'ind' may be used uninitialized [-Wmaybe-uninitialized]
   38 |             if(natural_lcm(answer[i],answer[j])<lcm || (natural_lcm(answer[i],answer[j])==lcm && answer[j]<answer[ind])){
      |                                                                                                                      ^
main.cpp:34:13: note: 'ind' was declared here
   34 |     int lcm,ind;
      |             ^~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;


int natural_gcd(int a,int b){
    int m,M;
    if(a>b){
        M=a;
        m=b;
    }else{
        M=b;
        m=a;
    }
    int temp;
    while(m>0){
        temp=m;
        m=M%m;
        M=temp;
    }
    return M;
}

int natural_lcm(int a,int b){
    return a*b/natural_gcd(a,b);
}

int main(){
    int n;
    cin>>n;
    vector<int> answer(n);
    for(int i=0;i<n;i++){
        cin>>answer[i];
    }
    int lcm,ind;
    for(int i=0;i<n-1;i++){
        lcm=INT_MAX;
        for(int j=i+1;j<n;j++){
            if(natural_lcm(answer[i],answer[j])<lcm || (natural_lcm(answer[i],answer[j])==lcm && answer[j]<answer[ind])){
                lcm=natural_lcm(answer[i],answer[j]);
                ind=j;
            }
        }
        lcm=answer[i+1];
        answer[i+1]=answer[ind];
        answer[ind]=lcm;
    }
    for(int i=0;i<n-1;i++){
        cout<<answer[i]<<" ";
    }
    cout<<answer[n-1]<<endl;
}

0