paulzhou的完美算术教室 数学

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值