using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Linq; using System.Collections; namespace CP.Common { /// /// 工具上用的一些常用方法 /// public class ToolUtils { /// /// 转换str里的中文数字为英文数字 /// /// /// public static string GetReplaceCnNumber(string str) { if (string.IsNullOrEmpty(str)) return ""; string[] cn = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" }; string[] en = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" }; for (int i = 0; i < cn.Length; i++) { if (str.IndexOf(cn[i], StringComparison.CurrentCultureIgnoreCase) != -1) str = str.Replace(cn[i], en[i]); } return str; } #region 组号方法 /// /// 双色球类的组号方式 /// /// /// /// public static List GetCombination(List reds, int m,string sm=null) { if (m == 1) return reds; if (string.IsNullOrEmpty(sm)) sm = ","; List result = new List(); if (reds.Count == m) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < reds.Count; i++) { sb.Append(reds[i]); if (i != (reds.Count - 1)) sb.Append(sm); } result.Add(sb.ToString()); return result; } string temp_firstelement = ""; //if (reds.Count > 0) //{ // temp_firstelement = reds[0]; // reds.RemoveAt(0); //} temp_firstelement = reds[0]; reds.RemoveAt(0); List temp_samplist1 = new List(); temp_samplist1.AddRange(reds); if (temp_samplist1.Count > 0) { List temp_list1 = GetCombination(temp_samplist1, m - 1, sm); foreach (string s in temp_list1) { result.Add(temp_firstelement + sm + s); } } List temp_samplist2 = new List(); temp_samplist2.AddRange(reds); if (temp_samplist2.Count > 0) { List temp_list2 = GetCombination(temp_samplist2, m, sm); result.AddRange(temp_list2); } return result; } #endregion #region 较通用的组选形态过滤 /// /// 组选形态过滤 /// /// /// /// public static List ZxFilter(string str, int zx, List list) { List temp = new List(); for (int i = 0; i < list.Count; i++) { int val = ToolUtils.GetZx(list[i].ToString("000")); if (str.IndexOf(val.ToString()) != -1) { temp.Add(list[i]); } } //组选方式输出 if (zx == 2) { List temp2 = new List(); for (int i = 0; i < temp.Count; i++) { List nums = ToolUtils.GetNumList(temp[i].ToString("000")); nums.Sort(); int tnum = Convert.ToInt32(nums[0].ToString() + nums[1].ToString() + nums[2].ToString()); if (!temp2.Contains(tnum)) temp2.Add(tnum); } return temp2; } return temp; } #endregion /// /// 两个list int中相同号码的个数 /// /// /// /// public static int GetSameNumTimes(List list1, List list2) { return list1.Intersect(list2).ToList().Count; } /// /// 根据012形态 /// 返回类似 012的数组 /// /// /// public static List Get012List(string xt) { List temp = new List(); List list = new List(); if (string.IsNullOrEmpty(xt)) return null; for (int i = 0; i < xt.Length; i++) temp.Add(xt.Substring(i, 1)); list.Add(temp[0] + temp[1] + temp[2]); list.Add(temp[0] + temp[2] + temp[1]); list.Add(temp[1] + temp[0] + temp[2]); list.Add(temp[1] + temp[2] + temp[0]); list.Add(temp[2] + temp[1] + temp[0]); list.Add(temp[2] + temp[0] + temp[1]); return list; } /// /// 根据质合形态 /// 返回类似 质质合的数组 /// /// /// public static List GetZhList(string xt) { List list = new List(); List temp = new List(); if (string.IsNullOrEmpty(xt)) return null; for (int i = 0; i < xt.Length; i++) temp.Add(xt.Substring(i, 1)); list.Add(temp[0] + temp[1] + temp[2]); list.Add(temp[0] + temp[2] + temp[1]); list.Add(temp[1] + temp[0] + temp[2]); list.Add(temp[1] + temp[2] + temp[0]); list.Add(temp[2] + temp[1] + temp[0]); list.Add(temp[2] + temp[0] + temp[1]); return list; } /// /// 根据奇偶形态 /// 返回类似 偶偶奇的数组 /// /// /// public static List GetJoList(string xt) { List list = new List(); List temp = new List(); if (string.IsNullOrEmpty(xt)) return null; for (int i = 0; i < xt.Length; i++) temp.Add(xt.Substring(i, 1)); list.Add(temp[0] + temp[1] + temp[2]); list.Add(temp[0] + temp[2] + temp[1]); list.Add(temp[1] + temp[0] + temp[2]); list.Add(temp[1] + temp[2] + temp[0]); list.Add(temp[2] + temp[1] + temp[0]); list.Add(temp[2] + temp[0] + temp[1]); return list; } /// /// 根据大小形态 /// 返回大小形态的组选数组 /// /// /// public static List GetDxList(string xt) { List list = new List(); List temp = new List(); if (string.IsNullOrEmpty(xt)) return null; for (int i = 0; i < xt.Length; i++) temp.Add(xt.Substring(i,1)); list.Add(temp[0] + temp[1] + temp[2]); list.Add(temp[0] + temp[2] + temp[1]); list.Add(temp[1] + temp[0] + temp[2]); list.Add(temp[1] + temp[2] + temp[0]); list.Add(temp[2] + temp[1] + temp[0]); list.Add(temp[2] + temp[0] + temp[1]); return list; } /// /// 返回某号码的所有组选形态列表 /// /// 号码 /// public static List GetZxNumIntList(string num) { List nums = GetNumList(num); List zx = new List(); zx.Add(TypeConverter.StrToInt(nums[0].ToString() + nums[1].ToString() + nums[2].ToString())); zx.Add(TypeConverter.StrToInt(nums[0].ToString() + nums[2].ToString() + nums[1].ToString())); zx.Add(TypeConverter.StrToInt(nums[1].ToString() + nums[0].ToString() + nums[2].ToString())); zx.Add(TypeConverter.StrToInt(nums[1].ToString() + nums[2].ToString() + nums[0].ToString())); zx.Add(TypeConverter.StrToInt(nums[2].ToString() + nums[0].ToString() + nums[1].ToString())); zx.Add(TypeConverter.StrToInt(nums[2].ToString() + nums[1].ToString() + nums[0].ToString())); return zx; } /// /// 返回组选形态的所有号码组合 /// /// /// public static List GetZxNumList(string num) { List nums = GetNumList(num); List zx = new List(); zx.Add(nums[0].ToString() + nums[1].ToString() + nums[2].ToString()); zx.Add(nums[0].ToString() + nums[2].ToString() + nums[1].ToString()); zx.Add(nums[1].ToString() + nums[0].ToString() + nums[2].ToString()); zx.Add(nums[1].ToString() + nums[2].ToString() + nums[0].ToString()); zx.Add(nums[2].ToString() + nums[0].ToString() + nums[1].ToString()); zx.Add(nums[2].ToString() + nums[1].ToString() + nums[0].ToString()); return zx; } /// /// 把某个数字拆分成1 2 3 的形式 /// 如980成9 8 0 /// /// /// public static List GetNumStringList(string num) { List nums = new List(); if (string.IsNullOrEmpty(num) || TypeConverter.StrToInt(num, -1) == -1) return nums; for (int i = 0; i < num.Length; i++) { nums.Add(num.Substring(i, 1)); } return nums; } /// /// 把某个数字拆分成1 2 3 的形式 /// 如980成9 8 0 /// /// /// public static List GetNumList(string num) { List nums = new List(); if (string.IsNullOrEmpty(num) || TypeConverter.StrToInt(num, -1) == -1) return nums; try { for (int i = 0; i < num.Length; i++) { nums.Add(TypeConverter.ObjectToInt(num.Substring(i, 1),0)); } } catch { nums = new List(); } return nums; } /// /// 广东11选5数据 /// /// /// public static List GetGd11x5NumList(string num) { List nums = new List(); string[] strs = num.Split(','); for (int i = 0; i < strs.Length; i++) { nums.Add(TypeConverter.ObjectToInt(strs[i], 0)); } return nums; } /// /// 某号码的二码和差值 /// 二码和按新计算方式 /// /// /// /// /// public static List GetNewEmHC(string num, int type,int anhc = 0) { List nums = GetNumList(num); List hc = new List(); int h1, h2, h3; List val = new List(); if (type == 0) //和 { h1 = nums[0] + nums[1]; h2 = nums[0] + nums[2]; h3 = nums[1] + nums[2]; } else //差 { h1 = Math.Abs(nums[0] - nums[1]); h2 = Math.Abs(nums[0] - nums[2]); h3 = Math.Abs(nums[1] - nums[2]); if (anhc>0) { val.Add(nums[0] + 10 - nums[1]); val.Add(nums[0] + 10 - nums[2]); val.Add(nums[1] + 10 - nums[0]); val.Add(nums[1] + 10 - nums[2]); val.Add(nums[2] + 10 - nums[0]); val.Add(nums[2] + 10 - nums[1]); } } hc.Add(h1); if (!hc.Contains(h2)) hc.Add(h2); if (!hc.Contains(h3)) hc.Add(h3); for (int i = 0; i < val.Count; i++) { if (!hc.Contains(val[i])) hc.Add(val[i]); } for (int i = 0; i < hc.Count; i++) { if (hc[i] > 9) hc[i] = hc[i] - 10; } return hc; } /// /// 取某号码的二码和或差 /// /// 号码 /// =0为二码和 =1为二码差 /// public static List GetEmHC(string num, int type) { List nums = GetNumList(num); List hc = new List(); int h1, h2, h3; if (type == 0) { h1 = nums[0] + nums[1]; h2 = nums[0] + nums[2]; h3 = nums[1] + nums[2]; } else { h1 = Math.Abs(nums[0] - nums[1]); h2 = Math.Abs(nums[0] - nums[2]); h3 = Math.Abs(nums[1] - nums[2]); } hc.Add(h1); if (!hc.Contains(h2)) hc.Add(h2); if (!hc.Contains(h3)) hc.Add(h3); return hc; } /// /// 返回某个号码的012路形态 /// /// /// public static string Get012Xt(string num) { StringBuilder xt = new StringBuilder(); List k = GetNumList(num); for (int i = 0; i < k.Count; i++) { xt.Append(k[i] % 3); } return xt.ToString(); } /// /// 返回某个号码的和值 /// /// /// public static int GetHz(string num) { List k = GetNumList(num); return k[0] + k[1] + k[2]; } /// /// 返回某个号码的和尾 /// /// /// public static int GetHw(string num) { int hz = GetHz(num); int hw = 0; if (hz < 10) hw = hz; if (hz >= 10 && hz < 20) hw = hz - 10; if (hz >= 20 && hz < 30) hw = hz - 20; return hw; } /// /// 号码的跨度 /// /// /// public static int GetKd(string num) { List k = GetNumList(num); k.Sort(); return Math.Abs(k[0] - k[k.Count - 1]); } /// /// 返回某个号码的大中小形态 /// /// /// public static string GetDzx(string num) { StringBuilder sb = new StringBuilder(); List k = GetNumList(num); foreach (int i in k) { if (i < 3) sb.Append("小"); if (i >= 3 && i < 7) sb.Append("中"); if (i >= 7) sb.Append("大"); } return sb.ToString(); } /// /// 大小形态 /// /// /// public static string GetDx(string num) { StringBuilder sb = new StringBuilder(); List k = GetNumList(num); foreach (int i in k) { if (i < 5) sb.Append("小"); if (i >=5) sb.Append("大"); } return sb.ToString(); } /// /// 奇偶形态 /// /// /// public static string GetJo(string num) { StringBuilder sb = new StringBuilder(); List k = GetNumList(num); foreach (int i in k) { if (i % 2 == 0) sb.Append("偶"); else sb.Append("奇"); } return sb.ToString(); } /// /// 质合 /// /// /// public static string GetZh(string num) { StringBuilder sb = new StringBuilder(); List k = GetNumList(num); foreach (int n in k) { if (n == 0 || n == 4 || n == 6 || n == 8 || n == 9) sb.Append("合"); else sb.Append("质"); } return sb.ToString(); } /// /// 返回某个号码的组选形态 /// /// /// 组六=1 组三=3组 豹子=10 public static int GetZx(string num) { if (string.IsNullOrEmpty(num)) return -1; int n = TypeConverter.StrToInt(num, -1); if (n <0) return -1; num = n.ToString("000"); List nums = GetNumList(num); nums.Sort(); int xt = 0; if (nums[0] == nums[1] && nums[1] == nums[2] && nums[0] == nums[2]) { //豹子 xt = 5; return xt; } if (nums[0] != nums[1] && nums[1] != nums[2] && nums[0] != nums[2]) { xt = 6; return xt; } if (nums[0] == nums[1] || nums[1] == nums[2] || nums[0] == nums[2]) { xt = 3; return xt; } return xt; } #region AC值计算 /// /// AC值计算 /// /// /// public static int GetAC(string[] kjh) { List result = GetCombination(kjh, 2); ArrayList acarray = new ArrayList(); int tpac = 0; for (int i = 0; i < result.Count; i++) { string[] tp = result[i].Split(','); int tmp = Math.Abs(Convert.ToInt32(tp[0]) - Convert.ToInt32(tp[1])); if (!acarray.Contains(tmp)) { tpac++; acarray.Add(tmp); } } return tpac - (kjh.Length - 1); } #endregion #region 组合算法 /// /// 组合算法 /// /// 组合源数据 /// 所选组合个数 /// public static List GetCombination(string[] data, int count) { Dictionary dic = new Dictionary(); List output = new List(); for (int i = 0; i < data.Length; i++) { dic.Add(data[i], i); } SelectN(dic, data, count, 1, ref output); return output; } /// /// 字典组合算法 /// /// /// /// /// /// private static void SelectN(Dictionary dd, string[] data, int count, int times, ref List output) { Dictionary dic = new Dictionary(); foreach (KeyValuePair kv in dd) { for (int i = kv.Value + 1; i < data.Length; i++) { if (times < count - 1) { dic.Add(kv.Key + "," + data[i], i); } else { output.Add(kv.Key + "," + data[i]); } } } times++; if (dic.Count > 0) { SelectN(dic, data, count, times, ref output); } } #endregion //end } }