这是问题所在: 我必须创建一个模拟城市的程序,特别是其建筑物,以确定其天际线和其他值的平均高度。我的问题是选择数据结构:我需要能够在不同年份计算这些值的东西:例如,用户可以在2016年询问天际线值,然后在2020年获得最高建筑物。
每个建筑物都有一个字符串作为键,但一旦它们被拆除,同样的键就可以再次使用。
这是大学的一个项目,所以我只想要一个小费;我需要知道更多“什么”和“为什么”而不是“如何”。
我使用Java。
感谢。
答案 0 :(得分:1)
您正在寻找某种类型的多级字典的地图。 您可以拥有一个2级字典,例如{2018:{height1,height2},2002:{} ......} 每个内部词典都是当年所有建筑物的高度(这是关键)。此外,字典很快(虽然你正在为空间交易)。
希望这有帮助!
答案 1 :(得分:0)
如果构建密钥是唯一的,那么您将拥有这样的类:
class Building
Key
Name
Height
YearBuilt
YearDemolished
你可以通过一系列建筑物的简单循环来解决你的问题:
for each building
if (building.yearBuilt <= year && building.yearDemolished >= year)
{
// the building was standing during that year
}
如果可以重复使用密钥,您仍需要保留历史信息。如果你把它与建筑物保持在一起,那么你会有类似的东西:
class HistoricalInfo
Name
Height
YearBuilt
YearDemolished
class Building
Key
HistoricalInfo[] // some collection (array, list, etc.)
你的循环变得有点复杂:
for each building
for each building.historyInfo
if (historyInfo.YearBuilt <= year && historyInfo.YearDemolished >= year)
{
// this instance of the key was standing during that year
}