DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Score", typeof(int));dt.Rows.Add(new object[]{"同学一", 79});
...
dt.Rows.Add(new object[]{"同学十", 90});DataRow[] rows = dt.Select("", "Score desc");rows[0][0] 是第一名的名字,rows[0][1] 是第一名的分数
rows[1][0] 是第二名的名字,rows[1][1] 是第二名的分数
........
类推,后面就不用我再罗嗦了吧。
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Score", typeof(int));dt.Rows.Add(new object[]{"同学一", 79});
...
dt.Rows.Add(new object[]{"同学十", 90});DataRow[] rows = dt.Select("", "Score desc");rows[0][0] 是第一名的名字,rows[0][1] 是第一名的分数
rows[1][0] 是第二名的名字,rows[1][1] 是第二名的分数
........
类推,后面就不用我再罗嗦了吧。
有多少个就执行多少次: dt.Rows.Add(new object[]{"同学N", 成绩N});>> 计算奖金、罚金需要再加一列
DataColumn col = dt.Columns.Add("Bonus", typeof(int));
col.DefaultValue = 100;//默认奖金//排序完毕后,计算奖金:
for (int i=0; i<5; i++)
{
int bouns = (int)(rows[i][2]));
rows[i][2] = bonus*(2-0.2*i);//前5名奖金递减
}//计算罚金
for (int i=0; i<5; i++)
{
int bouns = (int)(rows[rows.Length-i-1][2]));
rows[rows.Length-i-1][2] = bonus*(0.2*i);
}整体程序如下:
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Score", typeof(int));
DataColumn col = dt.Columns.Add("Bonus", typeof(int));
col.DefaultValue = 100;//默认奖金dt.Rows.Add(new object[]{"同学一", 79});
......
dt.Rows.Add(new object[]{"同学十", 90});
......
//有多少个就执行多少次: dt.Rows.Add(new object[]{"同学N", 成绩N});//排序
DataRow[] rows = dt.Select("", "Score desc");//排序完毕后,计算奖金:
for (int i=0; i<5; i++)
{
int bouns = (int)(rows[i][2]));
rows[i][2] = bonus*(2-0.2*i);//前5名奖金递减
}//计算罚金
for (int i=0; i<5; i++)
{
int bouns = (int)(rows[rows.Length-i-1][2]));
rows[rows.Length-i-1][2] = bonus*(0.2*i);
}......
然后把 rows 作为中间结果传给后面的计算...没有测试,不知道结果是不是正确的,相信思路应该没错吧。