結果

問題 No.135 とりあえず1次元の問題
ユーザー nukosuke
提出日時 2016-10-19 16:52:58
言語 C90
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 936 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 23,552 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-11 07:01:34
合計ジャッジ時間 910 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21 WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:20:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |   scanf("%d\n", &N);
      |   ^~~~~~~~~~~~~~~~~
main.c:25:3: warning: ignoring return value of ‘fgets’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   25 |   fgets(line, length, stdin);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int int_sort(const void *a, const void *b) {
  if( *( int * )a < *( int * )b ) {
    return -1;
  }
  else if( *( int * )a == *( int * )b ) {
    return 0;
  }
  return 1;
}

int main() {
  int N, *X;
  char *line;

  scanf("%d\n", &N);
  X = (int *)malloc(sizeof(int) * N);

  int length = 6*N + N-1;
  line = (char *)malloc(sizeof(char) * length);
  fgets(line, length, stdin);

  char *delim = " \n";
  char *tok;
  int i = 0;

  tok = strtok(line, delim);
  while(tok != NULL) {
    X[i] = atoi(tok);
    tok = strtok(NULL, delim);
    i++;
  }
  free(line);

  qsort(X, N, sizeof(int), int_sort);

  int answer = 1000001;
  for (i = 0; i < N-1; i++) {
    if (X[i] == X[i+1]) continue;
    int dist = X[i+1] - X[i];
    answer = dist < answer ? dist : answer;
  }

  if (answer == 1000001) answer = 0;
  printf("%d\n", answer);
  free(X);
  return 0;
}
0