結果

問題 No.716 距離
ユーザー monburan_0401monburan_0401
提出日時 2018-09-15 10:48:43
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,910 bytes
コンパイル時間 1,056 ms
コンパイル使用メモリ 29,664 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 23:21:42
合計ジャッジ時間 2,020 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 0 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 0 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 0 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 0 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// N個の点のうち、任意の2点の最大と最小を求めるプログラム
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main(void){
    int N;				//点の数
    int a[1000];		//位置の値を入れる配列
    int change;
    int max,min;		//2点の距離の最大値、最小値
    int d;
    
    scanf("%d",&N);
    for(int i = 0; i < N; i++){
        scanf("%d",&a[i]);
    }
    
    //	昇順に並べ替える
    for(int i = 0; i < N-1; i++){
    	for(int j = i; j < N; j++){
    		if(a[i] < a[j]){
    			change = a[i];
    			a[i] = a[j];
    			a[j] = change;
    		}
    	}
    }
    
    max = abs(a[N-1] - a[0]);
    min = 200000;       //  仮設定。minの最大
    
    for(int k = 0; k < N-1; k++){
        d = abs(a[k+1] - a[k]);
        if(min > d){
            min = d;
        }
    }
    
/*    //最大値と最小値を求める(最初は仮に設定)
    //二つの値の正負によって式が変わる
    //符号が異なる場合は片方の符号を入れ替える
    if(a[0] * a[1] < 0) {
    	a[0] *= -1;
    	max = abs(a[0] + a[1]);
    	a[0] *= -1;     //計算後に元の値に戻しておく事を忘れない
    } else {
    	max = abs(a[0] - a[1]);
    }
    min = max;
    for(i = 0; i < N - 1; i++){         //左側はN-2番目まで
        for(j = i + 1; j < N; j++){     //右側はN-1番目まで
            if(a[i] * a[j] < 0){
                a[i] *= -1;
                an = abs(a[i] + a[j]);
                a[i] *= -1;                   //計算後に元の値に戻しておく
            } else {
                an = abs(a[i] - a[j]);
            }
        	if(an > max){
        		max = an;
        	} 
        	if(an < min){
        		min = an;
        	}
        }
    }
    //ここまで来たら最大値・最小値は求まっているはず
*/
    printf("%d\n",min);
    printf("%d\n",max);
    return 0;
}
0