Description
众所周知,paulzhou的数学不太好。现在他有一个问题,希望你帮他解答:
在二维平面上给出一些整数点,希望在这些点中找出两个距离最近的点,并且输出这两个点的距离。
Input
第1行输入T(1≤T≤100),代表有T组数据。
接下来的T行输入,每行包含一组测试数据。输入数据为一系列坐标。数据保证为严格的“(x1, y1) (x2, y2) (x3, y3) … ”格式。输入保证点的数量不超过100个。坐标均为非负整数,且不会超过100,输入字符串长度不会超过1000。
Output
每组测试数据输出一行,仅包含一个浮点数,代表最近的距离,输出保留四位小数(无需四舍五入)。
Sample Input
2
(1, 1) (2, 1) (0, 0)
(1, 1) (2, 2)
Sample Output
1.0000
1.4142
Hint
题意
题解:
注意处理字符串的时候考虑数字超过一位的情况
AC代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define INF 100000007
char st[100005];
struct node{
double x,y;
}sp[1005];
double dis(node a,node b){
return sqrt((b.y-a.y)*(b.y-a.y)+(b.x-a.x)*(b.x-a.x));
}
int main(){
int n,t;
scanf("%d",&t);
getchar();
while (t--){
memset(st,0,sizeof(st));
//getchar();
gets(st);
//getchar();
int len = strlen(st);
int k = 0;
for (int i = 1; i < len; i+=7){
sp[k].x = st[i]-'0';
if (st[i+1]-'0'>=0&&st[i+1]-'0'<=9){
sp[k].x = (10*sp[k].x)+st[i+1]-'0'; i++;
}
if (st[i+1]-'0'>=0&&st[i+1]-'0'<=9){
sp[k].x = (10*sp[k].x)+st[i+1]-'0'; i++;
}
sp[k].y = st[i+3]-'0';
if (st[i+4]-'0'>=0&&st[i+4]-'0'<=9){
sp[k].y = (10*sp[k].y)+st[i+4]-'0'; i++;
}
if (st[i+4]-'0'>=0&&st[i+4]-'0'<=9){
sp[k].y = (10*sp[k].y)+st[i+4]-'0'; i++;
}
k++;
}
double mn = INF;
for (int i = 0; i < k; ++i){
for (int j = i+1; j < k; ++j){
mn = min(mn,dis(sp[i],sp[j]));
}
}
printf("%.4lf\n",mn);
}
return 0;
}