データの整列
Posted feedbacks - D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import std.stdio;
import std.algorithm;
void main(){
int[][] array = [[1, 3], [-1, 0], [-2, 3], [1, -9], [1, -19],
[4, 2], [3, 104], [4, -91]];
bool lexical(int[] a, int[] b){
return (a[0] == b[0]) ? (a[1] < b[1]) : (a[0] < b[0]);
}
bool distance(int[] a, int[] b){
return (a[0] * a[0] + a[1] * a[1]) < (b[0] * b[0] + b[1] * b[1]);
}
auto a1 = array.dup;
auto a2 = array.dup;
sort!(lexical)(a1);
sort!(distance)(a2);
writeln(a1); // [[-2 3] [-1 0] [1 -19] [1 -9] [1 3] [3 104] [4 -91] [4 2]]
writeln(a2); // [[-1 0] [1 3] [-2 3] [4 2] [1 -9] [1 -19] [4 -91] [3 104]]
}
|



odz #5839() Rating1/1=1.00
(x, y) の座標情報を以下の2種類の方法で整列する機能を実現してください。
データの表現方法はタプルなり構造体/オブジェクトなり各自で適当に選んで下さい。
[ reply ]