C# 双色球核心代码

这篇博客介绍了两个C#类,`DobuleChromosphere`和`Selector`,用于模拟彩票选号和计算投注金额。`DobuleChromosphere`类包含了红色球和蓝色球的列表,以及投注类型和价格。它还提供了计算价格的方法,基于网易彩票的算法。`Selector`类则用于生成随机的红色和蓝色球号码,并存储选中的号码。用户可以通过这两个类进行彩票号码的随机生成和投注金额的计算。
摘要由CSDN通过智能技术生成
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class DobuleChromosphere
    {
        //红色球蓝色球
        public List<string>RedBalls { get; set; }
        public List<string>BlueBalls{ get; set; }

        /// <summary>
        /// 投注方式
        /// </summary>
        public string BallType { get; set; }

        /// <summary>
        /// 价格默认是单式,一注是2元
        /// </summary>
        public int Price { get; set; } = 2;

        #region 构造方法

        /// <summary>
        /// 用于显示的红色球和蓝色球组合字符串
        /// </summary>
        public string BallsShow
        {
            get
            {
                string nums = string.Join(" ", RedBalls) + "|";
                if (BlueBalls.Count > 1)
                    return nums += " " + string.Join(" ", BlueBalls);
                else
                    return nums += " " + BlueBalls[0];
            }
        }

        public string PriceShow =>Price + "元";
        #endregion

        //构造方法(用于计算当前投注的金额)复式金额算法:A!/((A-6)!*6!)*B
        /// <summary>
        /// 
        /// </summary>
        /// <param name="redBalls">红色球集合</param>
        /// <param name="bluBalls">蓝色球集合</param>
        public DobuleChromosphere(List<string> redBalls, List<string> bluBalls)
        {
            this.RedBalls = redBalls;
            this.BlueBalls = BlueBalls;

            //投注的方式推算
            BallType = bluBalls.Count > 1 || redBalls.Count > 6 ? "复式" : "单式";

            //根根红色球和蓝色球开始计算价格(请根据网易彩票的算法实现)
            int a = redBalls.Count;
            int b = bluBalls.Count;

            if (a > 6)
            {
                Price = Factorial(a) / (Factorial(a - 6) * Factorial(6)) * b;
            }
            else
            {
                Price *= b;
            }
        }

        /// <summary>
        /// 递归实现阶乘
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        private int Factorial(int n)
        {
            if (n < 1) return 0;
            if (n == 1) return 1;
            return n * (Factorial(n - 1));
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    /// <summary>
    /// 选号器类
    /// </summary>
    public class Selector
    {
        //红色、蓝色球池
        public List<string> RedBallList { get; set; }
        public List<string> BlueBallList { get; set; }

        //存储选中的号码
        public List<DobuleChromosphere> SelectedBalls { get; set; } = new List<DobuleChromosphere>();
        private Random random = new Random();

        public Selector()
        {
            RedBallList = new List<string> { 
            "01","02","03","04","05","06","07","08","09","10",
            "11","12","13","14","15","16","17","18","19","20",
            "21","22","23","24","25","26","27","28","29","30",
            "31","32","33"
            };
            BlueBallList = new List<string> {
            "01","02","03","04","05","06","07","08","09","10",
            "11","12","13","14","15","16"
            };
        }

        #region 方法:生成随机的蓝色球和红色球。
        public List<string> CreateRedNum(int count)
        {
            List<string> numList = new List<string>();
            while (true)
            {
                if (numList.Count == count) break;//如果达到随机数个数,就跳出
                string num = RedBallList[random.Next(33)];
                if (numList.Contains(num)) continue;
                else
                    numList.Add(num);
            }
            return numList;
        }

        public List<string> CreateBlueNum(int count)
        {
            List<string> numList = new List<string>();
            while (true)
            {
                if (numList.Count == count) break;//如果达到随机数个数,就跳出
                string num = BlueBallList[random.Next(16)];
                if (numList.Contains(num)) continue;
                else
                    numList.Add(num);
            }
            return numList;
        }
        #endregion

        #region 扩展方法:打印、保存到数据库(选择性作业、自行完成
        #endregion
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值