中文题目位置: http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/chs/aevent2.mspx
英文解题: http://www.microsoft.com/technet/scriptcenter/funzone/games/solutions08/expssol02.mspx
题目并不难, 从skaters.txt文件中读取选手信息, 去掉每个人最高分和最低分, 计算平均分, 打印前三名选手.
我的代码一次从文件中读取一行信息, 一行信息就是比赛选手的信息, 并为此选手创建一个Hash对象. 因为输入的内容格式固定为: Ken Myer,55,66,76,67,59,70,54. 因此简单使用Split就能将其变化为数组. 数组的第一项就是选手名称, 接下来使用[Array]类型的静态方法Sort对数组积分部分进行排序, 这样选手信息就会变成: Ken Myer,54,55,59,66,67,70,76, 我们直接对数组第三项到第7项求平均值. 生成的Hash对象发送到管道中. Sort-Object为了能够对管道对象进行排序会收集所有选手的hash对象, 然后根据选手积分进行排序, 最后Select-Object选取最后3个对象, 输出也比较简单. 下面给出代码:
$Results
=
Get
-
Content
-
Path
'
C:Scriptsskaters.txt
'
-
r
1
|
`
%
{
$competitor
=
New
-
Object
-
TypeName HashTable
$tmp
=
$_
.
Split
(
'
,
'
);
$competitor
[
'
name
'
]
=
$tmp
[
0
];
[Array]
::
Sort
(
$tmp
,
1
,
$tmp
.
length
-
1
);
$competitor
[
'
score
'
]
=
([
int
]
$tmp
[
2
]
+
$tmp
[
3
]
+
$tmp
[
4
]
+
$tmp
[
5
]
+
$tmp
[
6
])
/
5
;
$competitor
}
|
Sort
-
Object @{expression
=
{
$_
.
score}}
|
Select
-
Object
-
Last
3

"
Gold medal:
"
+
$Results
[
2
]
.
name
+
"
,
"
+
$Results
[
2
]
.
score
"
Silver medal:
"
+
$Results
[
1
]
.
name
+
"
,
"
+
$Results
[
1
]
.
score
"
Bronze medal:
"
+
$Results
[
0
]
.
name
+
"
,
"
+
$Results
[
0
]
.
score