圆形路径上,求某两点的最大的距离。

搜狗2018笔试题,编程题是这样:一个环形区域,从小到大输入每个环形点上的角度,求其中距离最大的两个点。

写了半天,人家说最好用时间复杂度低的写,所以想用时间复杂度为N的方法,但是没写出来,边界不好控制,一直到交完卷了才写出来,测试了几个特殊的例子,感觉好像没问题。。。

输入数据,第一个为输入的数据个数,接下来,从小到大输入数据,求其中距离最大的两个点。因为是圆环状,所以可以用N的平法的方法,也可以稍微改进,用N*log2N的方法。我用的是N的方法,大体思路是:因为有序输入数据,所以,找到最小的数据很方便,然后以最小的数据点穿过圆心画直径,这是第一条线,将所有的数据映射到这条线上,最接近最小数据+180的点,为最终点,算出第一个最大值。然后以这条直径作垂直线,同时遍历一遍数据,找到映射到新直线上的两个最远点,这是第二个最终点,求出两个最终点的最大值,即为最远点。

用数字举个例子,比如输入数据,10.0000,  189.00000,  196.00000,  200.0000  则作第一条直线为,10 ----- 190 ,最远的两个点为10.0000   和  189.0000,然后作第二条直线,100 ----- 280,求出最远点,再算他们之间的最大值。这种方法,只需要遍历两遍数组,时间复杂度为N。个人觉得这个思路没问题,欢迎挑毛病。(本来笔试的时候,不应该用这么难受的方法的,其实用N*log2N 就行,哎,当时死心眼了,非得用这个,把自己写的很难受。。。。)

代码:

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;


void fun(vector<double> &ve,int n)
{
	double xbegin = ve[0];
	double xend = xbegin + 180;
	double min = xbegin;
	double max = xbegin;
	bool flag = false;
	if(ve[0] >= 180 || ve[n-1] <= 180)
	{
		flag = true;
	}
	for(int i = 0; i < n; ++i)
	{
		if(abs(max - (xend)) > abs(ve[i] - xend))
			max = ve[i];
	}
	double end = max - min > 180 ? 180 - (max - min - 180) : max - min;
	if(flag)
	{//判断有没有必要在进行垂直线的计算
		cout<<fixed<<setprecision(8)<<end<<endl;
		return ;
	}
	xbegin = xbegin + 90;
	xend = xend + 90;
	min = xend;
	max = xbegin;
	for(int i = 0; i < n; ++i)
	{
		//找最大值
		if(abs(max - xend) > abs(ve[i] - xend))
			max = ve[i];
		//找最小值
		if(abs(min - xbegin) > abs(ve[i] - xbegin))
			min = ve[i];
	}
	double tmp = max - min > 180 ? 180 - (max - min - 180) : max - min;
	end = end > tmp ? end : tmp;
	cout<<fixed<<setprecision(8)<<end<<endl;
}
int main()
{
	vector<double> ve;
	int n;
	cin>>n;
	double tmp;
	for(int i = 0; i < n; ++i)
	{
		cin>>tmp;
		ve.push_back(tmp);
	}
	fun(ve,n);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是在上述代码基础上绘制最优路径图的示例代码: ```python import random import math import matplotlib.pyplot as plt # 34个省会城市的坐标 cities = [ (1150, 1760), (630, 1660), (40, 2090), (750, 1100), (750, 2030), (1030, 2070), (1650, 650), (1490, 1630), (790, 2260), (710, 1310), (840, 550), (1170, 2300), (970, 1340), (510, 700), (750, 900), (1280, 1200), (230, 590), (460, 860), (1040, 950), (590, 1390), (830, 1770), (490, 500), (1840, 1240), (1260, 1500), (1280, 790), (490, 2130), (1460, 1420), (1260, 1910), (360, 1980), (230, 1100), (1310, 1000), (550, 1200), (590, 1950), (830, 1360) ] # 计算两个城市之间距离 def distance(city1, city2): return math.sqrt((city1[0] - city2[0]) ** 2 + (city1[1] - city2[1]) ** 2) # 计算路径的总长度 def total_distance(path): return sum(distance(cities[path[i]], cities[path[i + 1]]) for i in range(len(path) - 1)) + distance(cities[path[0]], cities[path[-1]]) # 模拟退火算法 def simulated_annealing(T=10000, Tmin=1e-8, alpha=0.99): # 随机初始化路径 path = list(range(len(cities))) random.shuffle(path) # 计算初始路径长度 energy = total_distance(path) # 记录最优解 best_path = path best_energy = energy # 降温过程 while T > Tmin: # 随机交换两个城市的位置 i, j = random.sample(range(len(cities)), 2) new_path = path[:] new_path[i], new_path[j] = new_path[j], new_path[i] # 计算新路径长度 new_energy = total_distance(new_path) # 计算能量差 delta = new_energy - energy # 如果新路径更优,则接受新路径 if delta < 0 or math.exp(-delta / T) > random.random(): path = new_path energy = new_energy # 更新最优解 if energy < best_energy: best_path = path best_energy = energy # 降温 T *= alpha return best_path, best_energy # 绘制最优路径图 best_path, best_energy = simulated_annealing() x = [cities[i][0] for i in best_path] + [cities[best_path[0]][0]] y = [cities[i][1] for i in best_path] + [cities[best_path[0]][1]] plt.plot(x, y, marker='o') plt.show() ``` 该代码使用matplotlib库绘制了最优路径图,其中x和y分别表示最优路径上城市的横纵坐标,最后一个点为起点,marker为圆形。运行该代码可以得到最优路径图,可以直观地看到最短路径经过了哪些城市。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值