110,473
社区成员
发帖
与我相关
我的任务
分享
int ps1 = int.Parse(PS1TB.Text);
int ps2 = int.Parse(PS2TB.Text);
int ps3 = int.Parse(PS3TB.Text);
//123算法
{
if (ps1 > ps2 && ps1 > ps3)
{
LB1.Text = "1";
if (ps2 > ps3)
{
LB2.Text = "2";
LB3.Text = "3";
}
else if (ps3 > ps2)
{
LB2.Text = "3";
LB3.Text = "2";
}
else
{
LB2.Text = "2";
LB3.Text = "2";
}
}
else if (ps2 > ps1 && ps2 > ps3)
{
LB2.Text = "1";
if (ps1 > ps3)
{
LB1.Text = "2";
LB3.Text = "3";
}
else if (ps3 > ps1)
{
LB1.Text = "3";
LB3.Text = "2";
}
else
{
LB1.Text = "2";
LB3.Text = "2";
}
}
else if (ps3 > ps1 && ps3 > ps2)
{
LB3.Text = "1";
if (ps1 > ps2)
{
LB1.Text = "2";
LB2.Text = "3";
}
else if (ps2 > ps1)
{
LB1.Text = "3";
LB2.Text = "2";
}
else
{
LB1.Text = "2";
LB2.Text = "2";
}
}
else if (ps1 == ps2 && ps1 > ps3)
{
LB1.Text = "1";
LB2.Text = "1";
LB3.Text = "2";
}
else if (ps1 == ps3 && ps1 > ps2)
{
LB1.Text = "1";
LB2.Text = "2";
LB3.Text = "1";
}
else if (ps2 == ps3 && ps2 > ps1)
{
LB1.Text = "2";
LB2.Text = "1";
LB3.Text = "1";
}
else
{
LB1.Text = "1";
LB2.Text = "1";
LB3.Text = "1";
}
}
var list = new List<KeyValuePair<int, Label>>(9);
list.Add(new KeyValuePair<int, Label>(int.Parse(PS1TB.Text), LB1));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS2TB.Text), LB2));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS3TB.Text), LB3));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS4TB.Text), LB4));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS5TB.Text), LB5));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS6TB.Text), LB6));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS7TB.Text), LB7));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS8TB.Text), LB8));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS9TB.Text), LB9));
list.Sort((x, y) => y.Key - x.Key);
list[0].Value.Text = "1";
var tempRank = 1;
var tempValue = list[0].Key;
for (int i = 1; i < list.Count; i++)
{
if (list[i].Key == tempValue)
{
list[i].Value.Text = tempRank.ToString();
}
else
{
tempRank += 1;
tempValue = list[i].Key;
list[i].Value.Text = tempRank.ToString();
}
}
[/quote]
哈哈.后来偶也发现了.升级一下就好了.完美解决
var list = new List<KeyValuePair<int, Label>>(9);
list.Add(new KeyValuePair<int, Label>(int.Parse(PS1TB.Text), LB1));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS2TB.Text), LB2));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS3TB.Text), LB3));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS4TB.Text), LB4));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS5TB.Text), LB5));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS6TB.Text), LB6));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS7TB.Text), LB7));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS8TB.Text), LB8));
list.Add(new KeyValuePair<int, Label>(int.Parse(PS9TB.Text), LB9));
list.Sort((x, y) => y.Key - x.Key);
list[0].Value.Text = "1";
var tempRank = 1;
var tempValue = list[0].Key;
for (int i = 1; i < list.Count; i++)
{
if (list[i].Key == tempValue)
{
list[i].Value.Text = tempRank.ToString();
}
else
{
tempRank += 1;
tempValue = list[i].Key;
list[i].Value.Text = tempRank.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
List<KeyValuePair<int, Label>> keyValues = new List<KeyValuePair<int, Label>>();
keyValues.Add(new KeyValuePair<int, Label>(Convert.ToInt32(this.textBox1.Text), this.label1));
keyValues.Add(new KeyValuePair<int, Label>(Convert.ToInt32(this.textBox2.Text), this.label2));
keyValues.Add(new KeyValuePair<int, Label>(Convert.ToInt32(this.textBox3.Text), this.label3));
keyValues.Add(new KeyValuePair<int, Label>(Convert.ToInt32(this.textBox4.Text), this.label4));
keyValues.Add(new KeyValuePair<int, Label>(Convert.ToInt32(this.textBox5.Text), this.label5));
keyValues.Sort((a, b) => { return b.Key - a.Key; });
//经典逆序
//for (int i = 0; i < keyValues.Count; i++)
// keyValues[i].Value.Text = keyValues[i].Value.Text.Split('-')[0] + "-" + (i + 1);
//并列排序
int level = keyValues[0].Key;
for (int i = 0, j = 1; i < keyValues.Count; i++)
{
if (level != keyValues[i].Key)
{
j++;
level = keyValues[i].Key;
}
keyValues[i].Value.Text = keyValues[i].Value.Text.Split('-')[0] + "-" + j;
}
}
var list = new List<(int,Label)>()
{
(int.Parse(PS1TB.Text),LB1),
(int.Parse(PS2TB.Text),LB2),
(int.Parse(PS3TB.Text),LB3),
(int.Parse(PS4TB.Text),LB4),
(int.Parse(PS5TB.Text),LB5),
(int.Parse(PS6TB.Text),LB6),
(int.Parse(PS7TB.Text),LB7),
(int.Parse(PS8TB.Text),LB8),
(int.Parse(PS9TB.Text),LB9),
};
list.Sort((x, y) => y.Item1 - x.Item1);
list[0].Item2.Text = "1";
var tempRank = 1;
var tempValue = list[0].Item1;
for (int i = 1; i < list.Count; i++)
{
(var value, var lb) = list[i];
if (value == tempValue)
{
lb.Text = tempRank.ToString();
}
else
{
tempRank += 1;
tempValue = value;
lb.Text = tempRank.ToString();
}
}