[IT]/C++
2차원 동적할당으로 좌표간의 거리 구하기
givemebro
2020. 3. 11. 10:12
반응형
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double comput(double x1, double x2, double y1, double y2) {
return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
}
int main() {
int size;
scanf("%d", &size);
double **point = (double**)malloc(sizeof(double*)*size);
for (int i = 0; i < size; i++) {
point[i] = (double*)malloc(sizeof(double) * 2);
}
for (int i = 0; i < size; i++) {
scanf("%lf %lf", &point[i][0], &point[i][1]);
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%.2f ",
comput(point[i][0], point[j][0], point[i][1], point[j][1]));
}
puts("");
}
for (int i = 0; i < size; i++) {
free(point[i]);
}
free(point);
return 0;
}
/*
1 1
1 2
2 1
5 4
6 4
6 5
*/

반응형