刚刚学习编程,这个问题应该怎么解决

求最近距离的药店。【问题描述】
一到冬季,感冒生病的人就变多。试根据下面的描述求出距离小明家最近的药店距离。假设小明家用二维平面下的坐标点来表示(x,y),小明家附近有n家药店(n<=10),n家药店距离小明家均不超过1000,用坐标点(xi,yi)表示,编写程序求得小明家到最近的药店距离。即对于任意不同的xi,xj,求出min{ sqrt((xi- x)(xi-x) + (yi-y)(yi-y) )}
同时,需要采用以下实现方式完成:
自定义实现Point类,同时实现构造,拷贝构造,析构三个函数。并统计整个程序中三种函数的调用次数。可格式参考如下:
class Point{ public:
int x:y:
Point(int a=0,int b=0){…}
Point(const Point &a)L)
~Point0{.…}

下面是一个可以解决这个问题的示例代码:

#include <iostream>
#include <cmath>

using namespace std;

// 定义 Point 类
class Point {
public:
    int x, y;
    // 构造函数
    Point(int a = 0, int b = 0) {
        x = a;
        y = b;
        cout << "Point 构造函数被调用" << endl;
    }
    // 拷贝构造函数
    Point(const Point &a) {
        x = a.x;
        y = a.y;
        cout << "Point 拷贝构造函数被调用" << endl;
    }
    // 析构函数
    ~Point() {
        cout << "Point 析构函数被调用" << endl;
    }
};

// 计算两点之间的距离
double distance(Point p1, Point p2) {
    return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}

int main() {
    int n;
    Point home(0, 0); // 小明家的坐标
    Point store(0, 0); // 药店的坐标

    cout << "请输入小明家的坐标:" << endl;
    cin >> home.x >> home.y;
    cout << "请输入药店的数量:" << endl;
    cin >> n;

    // 记录最小距离
    double min_distance = 1000000;

    for (int i = 0; i < n; i++) {
        cout << "请输入第 " << i+1 << " 家药店的坐标:" << endl;
        cin >> store.x >> store.y;

        // 计算两点之间的距离
        double d = distance(home, store);
        // 更新最小距离
        if (d < min_distance) {
            min_distance = d;
        }
    }

    cout << "距离最近的药店距离小明家 " << min_distance << " 单位" << endl;
    return 0;
}

在上面的代码中,我们定义了一个 Point 类,其中包含构造函数、拷贝构造函数和析构函数。

在主函数中,我们首先输入小明家的坐标,然后输入药店的数量。接着,我们通过循环输入药店的坐标,并计算与小明家之间的距离。如果距离小于最小距离,就更新最小距离。最后,输出最小距离即可。

代码实现和解释如下,望采纳

#include <iostream>
#include <cmath>

using namespace std;

class Point {
 public:
  int x, y;
  Point(int a = 0, int b = 0) {
    x = a;
    y = b;
  }
  Point(const Point &a) {
    x = a.x;
    y = a.y;
  }
  ~Point() {}
};

int main() {
  int n;
  cin >> n;

  Point home;
  cin >> home.x >> home.y;

  Point pharmacies[10];
  for (int i = 0; i < n; i++) {
    cin >> pharmacies[i].x >> pharmacies[i].y;
  }

  double min_distance = 1e9;
  for (int i = 0; i < n; i++) {
    double distance = sqrt((home.x - pharmacies[i].x) * (home.x - pharmacies[i].x) + (home.y - pharmacies[i].y) * (home.y - pharmacies[i].y));
    if (distance < min_distance) {
      min_distance = distance;
    }
  }

  cout << min_distance << endl;

  return 0;
}

上述程序中,我们定义了一个 Point 类来表示二维平面上的坐标点,同时实现了构造、拷贝构造和析构三个函数。在程序的主函数中,我们输入了小明家和药店的坐标,然后求出距离小明家最近的药店的距离并输出。

内部设置一个数值变量,每调取一次函数,变量数值+1

#include <iostream>
using namespace std;
class Point
{
protected:
    int x,y;
public:
    static int gz,kbgz,xg;
public:
    Point(int x=0,int y=0):x(x),y(y) {gz++;}
    Point(const Point &p) {x = p.x;y=p.y;kbgz++;}
    ~Point() {xg++;}
    double getDis(const Point &p) {return sqrt((x-p.x)*(x-p.x)*1.0 + (y-p.y)*(y-p.y));}
};
 
int Point::gz = 0;
int Point::kbgz = 0;
int Point::xg = 0;

int main()
{
    double minDis = 0;
    int x,y;
    cin>>x>>y;
    Point xm(x,y);
    //
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>x>>y;
        Point yd(x,y);
        double dis = xm.getDis(yd);
        if(i==0 || dis < minDis)
            minDis = dis;
    }
    cout<<"最短距离为:"<<minDis<<endl;
    cout<<"构造函数调用次数:"<<Point::gz<<endl;
    cout<<"拷贝构造函数调用次数:"<<Point::kbgz<<endl;
    cout<<"析构函数调用次数:"<<Point::xg<<endl;
    return 0;
}

就是求最短路径,网上例程够多了

回答不易,望采纳,有不明白的地方可以留言。谢谢。


#include <iostream>


using namespace std;

static int g_gzcs = 0;
static int g_xgcs = 0;
static int g_kbcs = 0;

class Point {
public:
    Point(int a = 0, int b = 0) {
        x = a;
        y = b;
        g_gzcs++;
    }
    ~Point(){
        g_xgcs++;
    }

    Point(const Point& p) {
        this->x = p.x;
        this->y = p.y;
        g_kbcs++;
    }

    int x = 0.0;
    int y = 0.0;
};

float getDistance(const Point& p1, const Point& p2) {
    //sqrt为求平方根,pow为求幂
    return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}

int main()
{
    int n = 0;      //药店数量
    Point xmPoint;  //小明家位置
    cout << "请输入小明家的位置:" << endl;
    cin >> xmPoint.x >> xmPoint.y;

    cout << "请输入周围药店数量:" << endl;
    cin >> n;

    if (n <= 0) {
        cout << "药店数量有误。" << endl;
        return 0;
    }
    float distance = INT_MAX;   //定义初始距离为最大值
    int index = 0;              //第几家药店最近
    for (int i = 0; i < n; i++)
    {
        Point ydPoint;
        cout << "请输入第"<< i + 1 << "家药店的位置:" << endl;
        cin >> ydPoint.x >> ydPoint.y;
        float t = getDistance(xmPoint, ydPoint);
        if (t < distance) {
            index = i + 1;
            distance = t;
        }
    }
    cout << "最短距离为:" << distance << endl;
    cout << "最短距离的是第" << index << "家药店。" << endl;
    cout << "构造函数调用次数:" << g_gzcs << endl;
    cout << "拷贝构造函数调用次数:" << g_kbcs << endl;
    cout << "析构函数调用次数:" << g_xgcs + 1 << endl;   //小明的位置的析构是在整个函数执行完成后才执行,所以此处要 + 1,加的是小明的位置的析构

    return 0;
}

结果如下:

img