求一段逻辑算法代码.之前可能输偶的不清楚.这里给上目前的例子代码

ljsheng 2020-01-10 06:43:51
有9个变量 int
N1=3;
N2=13;
N3=23;
N4=43;
N5=33;
N6=31;
N7=3;
N8=333;
N9=1113;
和9个lable1到9标签对应上面
怎么根据int变量给标签打上排序数字

比如N9最大对应的就是lable9=1
N1,N7最小就是等于 8(因为有2个一样的排名就1-8了)


主要逻辑不当当是处理排序而是要怎么打标签

比如算出来N9是最大那就是1 那任何给lable9.text赋值1
以此类推~~


怎么样可以(最好能给出完整代码.3Q..)


目前就做3个排名.要写怎么多...九个要疯了~



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";
}
}
...全文
354 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljsheng 2020-01-13
  • 打赏
  • 举报
回复
引用 18 楼 小了白了兔O.O 的回复:
[quote=引用 17 楼 小了白了兔O.O的回复:]可以用数组结合map的吧
我去,评论怎么删除啊[/quote] 你发违规了的吧.哈哈哈
吾友客 2020-01-13
  • 打赏
  • 举报
回复
放进数组,去重,排序即可
小了白了兔O.O 2020-01-12
  • 打赏
  • 举报
回复
引用 17 楼 小了白了兔O.O的回复:
可以用数组结合map的吧
我去,评论怎么删除啊
小了白了兔O.O 2020-01-12
  • 打赏
  • 举报
回复
可以用数组结合map的吧
ljsheng 2020-01-12
  • 打赏
  • 举报
回复
引用 10 楼 智者知已应修善业 的回复:
复制时弄了2个=号了没注意到,呵,LB1.Text = (aa.ToList().IndexOf(int.Parse(PS1TB.Text), 0) + 1).ToString();修改你应该懂得。
已解决~
ljsheng 2020-01-12
  • 打赏
  • 举报
回复
引用 12 楼 zhanghesky 的回复:
[quote=引用 8 楼 ljsheng 的回复:] 不知道为什么代码拷贝进去认不到函数 var list = new List<(int,Label)>() 提示预定义类型未定义或导入
你的版本低 可以换成KeyValuePair

            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] 哈哈.后来偶也发现了.升级一下就好了.完美解决
wanghui0380 2020-01-11
  • 打赏
  • 举报
回复
int[] aa = new int[9] { 44, 4, 77, 7, 33, 3, 99, 9, 66 }; aa.GroupBy(p=>p).OrderByDescending(p=>p.Key)
zhanghesky 2020-01-11
  • 打赏
  • 举报
回复
引用 8 楼 ljsheng 的回复:
不知道为什么代码拷贝进去认不到函数 var list = new List<(int,Label)>() 提示预定义类型未定义或导入
你的版本低 可以换成KeyValuePair

            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();
                }
            }
ljsheng 2020-01-10
  • 打赏
  • 举报
回复
引用 3 楼 zhanghesky 的回复:
自己改改,

           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();
                }
            }
不知道为什么代码拷贝进去认不到函数 var list = new List<(int,Label)>() 提示预定义类型未定义或导入
ljsheng 2020-01-10
  • 打赏
  • 举报
回复
引用 5 楼 huakai2418 的回复:
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;
            }
        }
有办法是赋值嘛..现在这样会变成累加.第一次计算是1第二才计算就变成11.偶改了下...没改明白
ljsheng 2020-01-10
  • 打赏
  • 举报
回复
引用 4 楼 智者知已应修善业 的回复:
你想要的界面效果图弄一个出来瞧瞧。
huakai2418 2020-01-10
  • 打赏
  • 举报
回复

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;
}
}
zhanghesky 2020-01-10
  • 打赏
  • 举报
回复
自己改改,

           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();
                }
            }
huakai2418 2020-01-10
  • 打赏
  • 举报
回复
你也14年的论坛寿数了,这个应该难不到你的,不过你的写的这个代码确实很啰嗦

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧