目录
个人主页:https://blog.csdn.net/ygb_1024?spm=1010.2135.3001.5421
欢迎志同道合者一起交流学习,我的QQ:94509325/微信号
相对排名(Relative Ranking)在现实中有很多应用,主要包括:
1、体育比赛排名:在体育比赛中,相对排名常常用于确定参赛队伍的表现和排名。例如,在足球比赛中,球队的相对排名可以根据每个球队的胜利场次、得分、失球等指标来确定。
2、学术排名:在学术领域,相对排名可以用于评估学校、学院、专业和教师的表现和声誉。例如,世界大学排名根据学校在学术研究、教育质量、国际化水平等方面的表现进行排名。
3、职业排名:在就业市场中,相对排名常常用于评估和比较不同职业的就业前景和薪酬水平。例如,根据薪酬、职业需求和工作满意度等指标,可以对不同职业进行相对排名。
4、商业排名:在商业领域,相对排名常常用于评估公司、品牌和产品的市场地位和声誉。例如,根据市场份额、营收、客户满意度等指标,可以对不同公司和品牌进行相对排名。
5、政府排名:在政府领域,相对排名可以用于评估政府机关和政府官员的绩效和公信力。例如,根据政府的治理能力、反腐败程度和公共服务水平等指标,可以对不同政府进行相对排名。
这些都是相对排名在现实中的一些应用,通过相对排名可以对不同实体进行比较和评估,从而帮助做出决策和制定策略。
1、相对排名:
1-1、Python:
- # 1.问题描述
- # 根据N名运动员的得分,找到相对等级和获得最高分前3名的人,分别获得金牌、银牌和铜牌;所有运动员的成绩都保证是独一无二的.
- # 2.问题示例:
- # 输入[6,3,7,2,1],输出["Silver Medal","Bronze Medal","Gold Medal","4","5"].
- # 3.代码实现:
- class Solution:
- # 参数nums: 整数列表
- # 返回值: 排序列表
- def findRelativeRanks(self, nums):
- # {}用于表示字典(dictionary),字典是一种无序的、可变的、可索引的数据结构,它由键值对组成.
- # []用于表示列表(list),列表是一种有序的、可变的、可索引的数据结构,它可以包含任意类型的元素.
- # ()用于表示元组(tuple),元组是一种有序的、不可变的、可索引的数据结构,它可以包含任意类型的元素.
- score = {}
- for i in range(len(nums)):
- score[nums[i]] = i
- sortedscore = sorted(nums, reverse=True)
- answer = [0] * len(nums) # 根据nums的长度,预留列表answer的元素占位数,即根据nums的长度,对列表answer进行初始化
- for i in range(len(sortedscore)):
- res = str(i + 1)
- if i == 0:
- res = 'Gold Medal'
- if i == 1:
- res = 'Silver Medal'
- if i == 2:
- res = 'Bronze Medal'
- answer[score[sortedscore[i]]] = res
- return answer
- # 主函数
- if __name__ == '__main__':
- num = [6, 3, 7, 2, 1]
- s = Solution()
- print("输入:", num)
- print("输出:", s.findRelativeRanks(num))
- # 4.运行结果:
- # 输入: [6, 3, 7, 2, 1]
- # 输出: ['Silver Medal', 'Bronze Medal', 'Gold Medal', '4', '5']
1-2、VBA:
- Rem 自定义函数findRelativeRanks,功能:实现相对排名
- Function findRelativeRanks(nums As Variant) As Variant
- Dim score As New Scripting.Dictionary '此处需要引用控件:Microsoft Scripting Runtime,即对应系统路径下的动态库:C:\Windows\SysWOW64\scrrun.dll
- Dim sortedscore As Variant
- Dim answer As Variant
- Dim i As Integer
- Dim res As String
-
- For i = LBound(nums) To UBound(nums)
- score.Add nums(i), i
- Next i
-
- sortedscore = SortDesc(nums)
- ReDim answer(LBound(nums) To UBound(nums))
-
- For i = LBound(sortedscore) To UBound(sortedscore)
- res = CStr(i + 1)
- If i = LBound(sortedscore) Then
- res = "Gold Medal"
- ElseIf i = LBound(sortedscore) + 1 Then
- res = "Silver Medal"
- ElseIf i = LBound(sortedscore) + 2 Then
- res = "Bronze Medal"
- End If
- answer(score(sortedscore(i))) = res
- Next i
-
- findRelativeRanks = answer
- End Function
- rem 自定义函数SortDesc,功能:实现数组的降序排列
- Function SortDesc(arr As Variant) As Variant
- Dim i As Integer, j As Integer
- Dim temp As Integer
-
- For i = LBound(arr) To UBound(arr)
- For j = i + 1 To UBound(arr)
- If arr(i) < arr(j) Then
- temp = arr(i)
- arr(i) = arr(j)
- arr(j) = temp
- End If
- Next j
- Next i
-
- SortDesc = arr
- End Function
- Rem 执行过程,功能:调用自定义函数findRelativeRanks,并在立即窗口中输出结果
- Sub TestRun()
- Dim num As Variant
- Dim result As Variant
-
- num = Array(6, 3, 7, 2, 1)
-
- result = findRelativeRanks(num)
-
- Debug.Print "输入: "; Join(num, ",")
- Debug.Print "输出: "; Join(result, ",")
- End Sub
-
-
- '输入: 7,6,3,2,1
- '输出: Silver Medal,Bronze Medal,Gold Medal,4,5
注意:1-2中的代码需粘贴到你的VBA编辑器中,按F5执行TestRun程序,结果会展示在立即窗口中。
2、相关文章:
2-3、Python-VBA编程500例-005-01(入门级)
2-4、Python-VBA编程500例-005-02(入门级)
Python算法之旅:http://iyenn.com/rec/1699032.html?spm=1001.2014.3001.5502
个人主页:https://blog.csdn.net/ygb_1024?spm=1010.2135.3001.5421
欢迎志同道合者一起交流学习,我的QQ:94509325/微信号:



评论记录:
回复评论: