小白纯记录
#include<iostream>
using namespace std;
#include<string>
#include<cmath>
#define MAX 100
int main() {
int x = 0, y = 0;
cout << "请输入你的位置x,y的值" << endl;
cin >> x >> y;
cout << "("<<x << "," << y <<")" << endl;
int num = 0;
cout << "请输入数组point的个数" << endl;
cin >> num;
int arr[MAX][MAX];
int M_dis[MAX];
int n = 0;
int min_dis = 1000000;
int** p = new int* [num];
for (int i = 0; i < num; i++)
{
//为每一行分配地址
p[i] = new int[2];
cout << "请输入point" << i + 1 << "的坐标" << endl;
cin >> p[i][0] >> p[i][1];
//cout << "p的x坐标为" << p[i][0]<<"p的y坐标为"<< p[i][1] << endl;
if (p[i][0] == x || p[i][1] == y)
{
cout << "point" << i + 1 << "为有效点" << endl;
arr[n][0] = p[i][0];
arr[n][1] = p[i][1];
//cout <<"坐标为("<< arr[j][0] << "," << arr[j][0] <<")" << endl;
M_dis[n] = abs(arr[n][0] - x) + abs(arr[n][1] - y);
cout << "曼哈顿距离为" << M_dis[n] << endl;
if (M_dis[n] < min_dis) {
min_dis = M_dis[n];
}
n++;
}
else {
cout << "point" << i + 1 << "不为有效点" << endl;
}
}
if (n != 0)
{
int min_xb = arr[0][0];
for (int i = 0; i < n; i++)
{
if (M_dis[i] == min_dis)
{
min_xb = i;
break;
}
}
cout << "共有" << n << "个有效点" << endl;
cout << "最小都哈曼数为" << min_dis << endl;
cout << "对应的点为(" << arr[min_xb][0] << "," << arr[min_xb][1] <<")" << endl;
}
else {
return -1;
}
system("pause");
return 0;
}