//我遍歷每個學生之間的距離,當然這樣直接逾時了,想請問有什麼演算法可以解決這個呢?提供關鍵字就好謝謝!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
struct student{
int x;
int y;
};
typedef struct student STU;
float CalDistance(STU stu1, STU stu2);
void Rounding(float num);
int main(){
int N,m,i,j,x,y;
float distance=5000.0, temp;
scanf("%d%d",&N,&m);
STU *students=(STU *) malloc(m*sizeof(STU));
for(i=0;i<m;i++){
scanf("%d%d",&x,&y);
students[i].x=x;
students[i].y=y;
}
// printf("Distance=%f\n", CalDistance(students[0],students[1]));
for(i=0;i<m;i++){
for(j=i+1;j<m;j++){
temp=CalDistance(students[i], students[j]);
if(temp<distance)
distance=temp;
}
}
Rounding(distance);
return 0;
}
float CalDistance(STU stu1, STU stu2){
float x1,x2,y1,y2;
x1=stu1.x;
x2=stu2.x;
y1=stu1.y;
y2=stu2.y;
return sqrt(pow(x1-x2, 2)+pow(y1-y2, 2));
}
void Rounding(float num){
float NewNum;
NewNum=(int) (num*10000+0.5)/10000.00;
printf("%0.4f", NewNum);
}
//我遍歷每個學生之間的距離,當然這樣直接逾時了,想請問有什麼演算法可以解決這個呢?提供關鍵字就好謝謝!
???
沒有啊,AC (2ms, 124KB),哪裡逾時了?