微信红包怎么实现

群里都在玩抢红包,抢了再发,发了再抢,简直是无聊,程序员感兴趣是的如何实现,这里简单说说实现思路,附上dome,代码有点low,好在是实现了
比如:
100块发30个红包========
微信红包怎么实现_第1张图片微信红包怎么实现_第2张图片微信红包怎么实现_第3张图片微信红包怎么实现_第4张图片微信红包怎么实现_第5张图片

1块发10个红包========
微信红包怎么实现_第6张图片
1块发10个红包========
微信红包怎么实现_第7张图片

=========================================================

发红包需要满足以下几个条件

1.总金额不变

2.每个红包都必须有钱

3.尽量的均匀点,不然抢红包没什么意思了

=========================================================

实现思路

1.首先要确定最小单位,这里是精确到分,我这里以int类型进行计算,得出的结果也全是int类型

2.数据均匀,这里以 1

3.余数处理,当数据平均给20个红包,肯定跟总金额有所偏差,这时候我们就要多退少补,如果大于总金额,让红包金额多的去减少(总金额/总人数),如果还有剩余,就让第二多的去减少,直到补完这个空缺为止,反之亦然.

4.打乱顺序,细心的园友可能发现,红包是有顺序规律的,我们必须简单的写个function去打乱它.

=========================================================

demo

 /// 
            /// 抢红包
            /// 
            /// 
            /// 
            /// 
            public List qhb(int money, int num)
            {
                int min = 1;
                int pjs = money / num;
                List list = new List();
                Random rnd = new Random();
                for (int i = 0; i < num; i++)
                {
                    int max = (money / (num - i)) * 2;
                    int s = rnd.Next(min, max);
                    s = checkmoney(s, min, max);
                    list.Add(s);
                    money -= s;
                }
                //前面数据都是合理的,结尾必须多退少补
                list = checklist(money, pjs, list);
                return list;
            }


    /// 
            /// 获取到合理数据
            /// 
            /// 
            /// 
            /// 
            /// 
            public int checkmoney(int i, int min, int max)
            {
                if (i > max || i < min)
                {
                    Random rnd = new Random();
                    i = rnd.Next(min, max);
                    return checkmoney(i, min, max);
                }
                return i;
            }




/// 
        /// 余数处理
        /// 
        /// 
        /// 
        /// 
        public List checklist(int money, int pjs, List list)
        {
            if (money != 0)
            {
                if (money > 0)
                {

                    List list_order = maopao(list);
                    //给最小
                    if (money / pjs == 0)
                    {                      
                        list_order[0] += money;
                    }
                    else
                    {
                        for (int i = 0; i < money / pjs + 1; i++)
                        {
                            if (i == money / pjs)
                            {
                                list_order[i] += (money - (money / pjs) * pjs);
                            }
                            else
                            {
                                list_order[i] += pjs;
                            }

                        }
                    }
                    return list_order;
                }
                else
                {

                    List list_order = maopao(list,"desc");
                    //给最大
                    if (money / pjs == 0)
                    {

                        list_order[0] += money;
                    }
                    else
                    {
                        for (int i = 0; i < -(money / pjs) + 1; i++)
                        {
                            if (i == -(money / pjs))
                            {
                                list_order[i] += (money - (money / pjs) * pjs);
                            }
                            else
                            {
                                list_order[i] -= pjs;
                            }

                        }
                    }
                    return list_order;
                }
            }
            return list;
        }




/// 
        /// 冒泡排序
        /// 
        /// 
        /// 
        /// 
        public List maopao(List list,string order = "asc")
        {
            if (order != "asc")
            {
                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < list.Count - i - 1; j++)
                    {
                        if (list[j + 1] > list[j])
                        {
                            int temp = list[j];
                            list[j] = list[j + 1];
                            list[j + 1] = temp;

                        }
                    }
                }
            }
            else 
            {
                for (int i = 0; i < list.Count; i++)
                {
                    for (int j = 0; j < list.Count-i-1;j++)
                    {
                        if (list[j+1] < list[j])
                        {
                            int temp = list[j];
                            list[j] = list[j+1];
                            list[j+1] = temp;

                        }
                    }
                }
            }
            return list;
        }




/// 
        /// 打乱顺序
        /// 
        /// 
        /// 
        public List suiji(List list) 
        {
            Random rnd = new Random();
            for (int i = 0; i < list.Count; i++)
            {
                int temp = list[i];
                int j = rnd.Next(0, list.Count-1);
                list[i] = list[j];
                list[j] = temp;
            }
            return list;
        }

你可能感兴趣的:(微信红包怎么实现)