• 模拟退火求最小覆盖圆和最小覆盖球


    //最小覆盖球
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <math.h>
    #define N 150
    #define eps 1e-8
    #define T 100
    #define delta 0.98
    #define INF 1e99
     
    using namespace std;
     
    struct Point
    {
        double x, y, z;
    };
     
    Point p[N];
     
    double dist(Point A, Point B)
    {
        return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y) + (A.z - B.z) * (A.z - B.z));
    }
     
    double Search(Point p[], int n)
    {
        Point s;
        s.x=s.y=s.z=0;
        double t = 10000;
        double ans = INF;
        while(t > eps)
        {
            int k = 0;
            for(int i = 0; i < n; i++)
                if(dist(s, p[i]) > dist(s, p[k]))
                    k = i;
            double d = dist(s, p[k]);
            ans = min(ans, d);
            s.x += (p[k].x - s.x) / d * t;
            s.y += (p[k].y - s.y) / d * t;
            s.z += (p[k].z - s.z) / d * t;
            t *= delta;
        }
        return ans;
    }
     
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i = 0; i < n; i++)
                cin >> p[i].x >> p[i].y >> p[i].z;
            double ans = Search(p, n);
            printf("%.7lf
    ", ans);
        }
        return 0;
    }
    //最小覆盖圆
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <math.h>
     
    #define N 1000
    #define eps 1e-8
     
    #define delta 0.98
    #define INF 1e99
     
    using namespace std;
     
    struct Point
    {
        double x, y, z;
    };
     
    Point p[N];
     
    double dist(Point A, Point B)
    {
        return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
    }
     
    double Search(Point p[], int n)
    {
        Point s;
        s.x=s.y=s.z=0;
        double t = 1000;
        double ans = INF;
        while(t > eps)
        {
            int k = 0;
            for(int i = 0; i < n; i++)
                if(dist(s, p[i]) > dist(s, p[k]))
                    k = i;
            double d = dist(s, p[k]);
            ans = min(ans, d);
            s.x += (p[k].x - s.x) / d * t;
            s.y += (p[k].y - s.y) / d * t;
        //    s.z += (p[k].z - s.z) / d * t;
            t *= delta;
        }
        printf("%.2f %.2f %.2f
    ",s.x,s.y,ans);
    }
     
    int main()
    {
        int n;
        while(scanf("%d",&n)&&n!=0)
        {
            for(int i = 0; i < n; i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
            double ans = Search(p, n);
        }
        return 0;
    }
    rush!
  • 相关阅读:
    gulp
    grunt
    iscroll手册
    Javascript闭包演示【转】
    【转】Backbone.js学习笔记(二)细说MVC
    【转】Backbone.js学习笔记(一)
    node包管理相关
    写出高效率的正则表达式的几点建议
    常用正则表达式
    help、man和info工具的区别
  • 原文地址:https://www.cnblogs.com/LH2000/p/14977487.html
Copyright © 2020-2023  润新知