結果

問題 No.14 最小公倍数ソート
ユーザー yaoshimaxyaoshimax
提出日時 2015-02-10 14:30:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,332 ms / 5,000 ms
コード長 959 bytes
コンパイル時間 1,377 ms
コンパイル使用メモリ 86,004 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 22:44:30
合計ジャッジ時間 49,297 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 46 ms
4,380 KB
testcase_04 AC 4,332 ms
4,380 KB
testcase_05 AC 1,446 ms
4,376 KB
testcase_06 AC 1,700 ms
4,376 KB
testcase_07 AC 2,212 ms
4,376 KB
testcase_08 AC 2,934 ms
4,376 KB
testcase_09 AC 3,961 ms
4,380 KB
testcase_10 AC 3,904 ms
4,380 KB
testcase_11 AC 4,049 ms
4,380 KB
testcase_12 AC 4,138 ms
4,380 KB
testcase_13 AC 4,173 ms
4,376 KB
testcase_14 AC 4,091 ms
4,376 KB
testcase_15 AC 4,221 ms
4,380 KB
testcase_16 AC 1,567 ms
4,376 KB
testcase_17 AC 1,125 ms
4,380 KB
testcase_18 AC 501 ms
4,380 KB
testcase_19 AC 2,667 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>

using namespace std;
int gcd( int a, int b){
  if( a > b ) return gcd(b,a);
  if( b%a==0 ) return a;
  return gcd( b%a, a);
}
int main(){
  int N;
  scanf("%d",&N);
  int a[N];
  for( int i = 0 ; i <N; i++) scanf("%d", &a[i]);
  for( int i = 0 ; i <N; i++){
    int maxVal = INT_MAX;
    int nextInd = i+1;
    for( int j = i+1; j<N; j++){
      int val= a[i]*a[j]/gcd(a[i],a[j]);
      if( val < maxVal || (val==maxVal && a[nextInd] > a[j])){
        maxVal=val;
        nextInd=j;
      }
    }
    swap(a[i+1],a[nextInd]);
  }
  for( int i = 0 ; i <N; i++ )printf("%d ",a[i]);
  printf("\n");
  return 0;
}
0