现在的位置: 首页 > 综合 > 正文

彩票复式投注时,计算注数的程序(C#)

2018年01月22日 ⁄ 综合 ⁄ 共 3384字 ⁄ 字号 评论关闭

做了一个彩票程序,为了复式投注颇费了点脑筋,网上找了半天,才知道这个公式,比如36选7,如果买10个号,公式就是:

10!/ (10-7)!/ 7!

 

顺手做了一个测试程序:

  1. using System;
  2. public class Test{
  3.     public static void Main(string[] args){
  4.         int a = 5;  // 选择了几 
  5.         int b = 2;
  6.         try{
  7.             if(args.Length == 1){
  8.                 b = int.Parse(args[0]);
  9.             } else if(args.Length >= 2){
  10.                 a = int.Parse(args[0]);
  11.                 b = int.Parse(args[1]);
  12.             }
  13.         }catch{
  14.             Console.WriteLine("请正确输入");
  15.             return;
  16.         }
  17.         
  18.         int d = GetCount(a, b);
  19.         Console.WriteLine("{0},{1},{2}", a,b,d);
  20.     }
  21.     
  22.     //buyNum : 购买的数字个数 
  23.     //perNum : 每注要求的数字个数 
  24.     // 返回值为 : buyNum!/(buyNum-perNum)!/perNum! 
  25.     public static int GetCount(int buyNum, int perNum){
  26.         if(perNum == buyNum)
  27.             return 1;
  28.             
  29.         if(perNum > buyNum){
  30.             throw new Exception("后者必须比前者小!");
  31.         }else if(perNum <= 0){
  32.             throw new Exception("后者不能小于0!");
  33.         }
  34.         
  35.         // 这3行能提高一点效率,并降低一部分溢出的可能 
  36.         int diff = buyNum - perNum;
  37.         if (diff > perNum)
  38.             perNum = diff;
  39.             
  40.         int result = 1;
  41.         for(int i=perNum + 1;i<=buyNum;i++){
  42.             result *= i;
  43.         }
  44.         
  45.         for(int i=1;i<=buyNum - perNum;i++){
  46.             result /= i;
  47.         }
  48.         return result;
  49.         /*
  50.          8个号码选7=   8注
  51.          9个号码选7=  36注
  52.         10个号码选7= 120注
  53.         11个号码选7= 330注
  54.         12个号码选7= 792注
  55.         */
  56.     }
  57. }

下面是生成复式投注时,列出组合号码的列表的代码:

  1. /// <summary>
  2. /// 从数组中查找s个数字的组合,并返回全部组合(根据复式彩票投注的号码,返回对应的全部号码组列表)
  3. /// </summary>
  4. /// <param name="arr">数组(购买的数字列表)</param>
  5. /// <param name="s">大于0的整数(每注要求的数字个数)</param>
  6. /// <returns></returns>
  7. List<List<int>> GetCountList(int[] arr, int s)
  8. {
  9.     if (s < 1)
  10.         throw new Exception("选择个数不能小于1!");
  11.     if (arr == null || arr.Length == 0 || arr.Length < s)
  12.         throw new Exception("数组不能为空 或 小于必选个数!");
  13.     if (arr.Length == s)
  14.     {
  15.         #region 数组长度等于s时,直接返回1个组合
  16.         List<List<int>> l_ret = new List<List<int>>();
  17.         List<int> l_item = new List<int>();
  18.         foreach (int item in arr)
  19.         {
  20.             l_item.Add(item);
  21.         }
  22.         l_ret.Add(l_item);
  23.         #endregion
  24.         return l_ret;
  25.     }
  26.     if (s == 1)
  27.     {
  28.         #region 从数组中查找1个数字的组合,直接把数组各元素返回
  29.         List<List<int>> l_ret = new List<List<int>>();
  30.         foreach (int item in arr)
  31.         {
  32.             List<int> l_item = new List<int>();
  33.             l_item.Add(item);
  34.             l_ret.Add(l_item);
  35.         }
  36.         #endregion
  37.         return l_ret;
  38.     }
  39.     List<List<int>> l_arrTmp = new List<List<int>>();
  40.     for (int i = 0; i < arr.Length - s; i++)
  41.     {
  42.         // 后面的数任意取s-1个
  43.         int[] tmp = GetArray(arr, i);
  44.         List<List<int>> l_arrTmp1 = GetCountList(tmp, s - 1);
  45.         foreach (List<int> item in l_arrTmp1)
  46.         {
  47.             item.Insert(0, arr[i]);
  48.             l_arrTmp.Add(item);
  49.         }
  50.     }
  51.     List<int> l_lastItem = new List<int>();
  52.     for (int i = arr.Length - s; i < arr.Length; i++)
  53.     {
  54.         l_lastItem.Add(arr[i]);
  55.     }
  56.     l_arrTmp.Add(l_lastItem);
  57.     return l_arrTmp;
  58. }
  59. /// <summary>
  60. /// 把arr数组中,位置为beg之后的元素全部放入一个新数组返回(不包含beg元素)
  61. /// </summary>
  62. /// <param name="arr"></param>
  63. /// <param name="beg"></param>
  64. /// <returns></returns>
  65. private static int[] GetArray(int[] arr, int beg)
  66. {
  67.     int[] tmp = new int[arr.Length - (beg + 1)];
  68.     for (int i = beg + 1; i < arr.Length; i++)
  69.         tmp[i - (beg + 1)] = arr[i];
  70.     return tmp;
  71. }

 

 

 

 

 

 

抱歉!评论已关闭.