且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

算法:找到村庄之间的平均距离

更新时间:2023-02-26 20:39:22

这是你的作业,所以我不会给你任何代码!



首先考虑如何手动完成。假设你有四个村庄,并找出你要放置的地方。

你会如何手动完成?那么,你首先假设它是在1号村,然后计算出彼此村庄的距离。取这些距离的平均值。

然后对村庄2,村庄3和村庄4做同样的事情 - 你现在有四个平均距离,可以计算出最低的距离。

那是你放邮局的村庄!



现在开始考虑如何自动化这个过程,然后开始思考关于实现它。


引用:

我正在试图弄清楚它是如何设计的但是我我很困惑。



不!你正在做的是试图神奇地猜测设计。不幸的是,魔法仅适用于绝地武士。



唯一的办法是使用一张纸,一支铅笔并手工解决问题。

当您需要了解数学时,请尝试将邮局放在每个村庄并获得每个村的分数。

再次尝试不同的村庄,绘制村庄的位置,看看你是否找到了一种效率更高的方法。

'尝试每个村庄'都是'强力'方法,有时,它是***的。



我们不做你的HomeWork。

HomeWork不会测试你乞求别人做你的工作的技能,它会让你思考并帮助你的老师检查您对所学课程的理解以及您应用这些课程时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。


试试这个 java - 设计一种有效的算法来查找最小化平均距离的邮局位置 - Stack Overflow [ ^ ]

Let x1< x2 < . . . < xn be real numbers representing coordinates of n villages located along a straight road. A post office needs to be built in one of these villages.

a) Design an efficient algorithm to find the post-office location minimizing the average distance between the villages and the post office.

What I have tried:

I'm trying to figure out how design it but i'm so confused. Can anyone help??

This is your homework, so I'll give you no code!

Start by thinking how you would do it manually. Assume you had four villages, and work out where you would place it.
How would you do that manually? Well, you'd start by assuming it was in Village 1, then work out the distance to each other village. Take the average of those distances.
Then do the same for Village 2, Village 3, and Village 4 - you now have four average distances and can work out which is the lowest.
That's the village where you put your post office!

Now start thinking about how you would automate that process, and then start to think about implementing it.


Quote:

I'm trying to figure out how design it but i'm so confused.


No! What you are doing is trying to magically guess the design. Unfortunately, magic works only for Jedi knights.

The only way to go is with a sheet of paper , pencil and solve the problem by hand.
As you need to understand the maths, try to put the post office in every village and get the score for each.
Try again with different sets of villages, make a drawing of positions of villages, see if you find a method that is more efficient.
The 'try every villages' is 'brut force' method, sometimes, it is the best.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.


try this java - Design an efficient algorithm to find the post-office location minimizing the average distance - Stack Overflow[^]