【发布时间】:2023-03-12 19:23:01
【问题描述】:
我在下面有这个输入,它显示了一个人在什么年龄有什么分数。
这存储在像这样Map<Person, Information> 的HashMap 中,Person 类只有double: getScore(),它返回分数,而类Information 有int:getAge(),它返回年龄。类中没有属性名。
{Person has at Age: 12 (Score: 50)
=alex,
Person has at Age: 16 (Score: 50)
=miki,
Person has at Age: 5 (Score: 100)
=shi,
Person has at Age: 4 (Score: 50)
=rafi,
Person has at Age: 1 (Score: 50). (Score: 50)
=sharbel,
Person has at Age: 5 (Score: 0). (Score: 0)
=thomas,
Person has at Age: 14 (Score: 60). (Score: 60)
=thomy,
Person has at Age: 14 (Score: 50). (Score: 50)
=angelos,
Person has at Age: 11 (Score: 50). (Score: 50)
=musti,
Person has at Age: 11 (Score: 100). (Score: 100)
=aloo,
Person has at Age: 2 (Score: 50). (Score: 50)
=evi}
我需要的是,将年龄相同且得分最高的用户分组。预期的输出应该是这样的:
{Person has at Age: 12 (Score: 50)
=alex,
Person has at Age: 16 (Score: 50)
=miki,
Person has at Age: 5 (Score: 100)
=[shi,thomas], // those are together
Person has at Age: 4 (Score: 50)
=rafi,
Person has at Age: 1 (Score: 50)
=sharbel,
Person has at Age: 14 (Score: 60).
=[thomy , angelos], // those are together and we consider the biggest score 60
Person has at Age: 11 (Score: 100)
=[musti, aloo], // those are together and we consider the biggest score 100
Person has at Age: 2 (Score: 50)
=evi}
所以请注意[thomy, angelos] 和[musti, aloo] 在一起,这是因为他们的年龄相同,我们认为他们之间的得分最大。
我尝试了许多不同的方法,但都没有成功,因此我没有尝试任何实现。
【问题讨论】:
-
请更正此语句“需要什么才能获得具有相同边缘但名称相同的人的最高分,因此预期的输出应该是这样的”。
-
您可以遍历每个年龄的地图,为地图中的每个人调用
getAge(),然后使用getScore()获取分数。计算得分最高的人并将其放入List<Name>。 -
@Kshitij 你能用java代码发表你的想法吗?
-
为什么 5 岁的人不在您的输出部分中的组中?
-
@jnrdn0011 更新了,谢谢我忘记了
标签: java arrays algorithm hashmap