123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Text;
- using System.Linq;
- using System.Collections;
- namespace CP.Common
- {
-
-
-
- public class ToolUtils
- {
-
-
-
-
-
- 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<string> GetCombination(List<string> reds, int m,string sm=null)
- {
- if (m == 1)
- return reds;
- if (string.IsNullOrEmpty(sm))
- sm = ",";
- List<string> result = new List<string>();
- 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 = "";
-
-
-
-
-
- temp_firstelement = reds[0];
- reds.RemoveAt(0);
- List<string> temp_samplist1 = new List<string>();
- temp_samplist1.AddRange(reds);
- if (temp_samplist1.Count > 0)
- {
- List<string> temp_list1 = GetCombination(temp_samplist1, m - 1, sm);
- foreach (string s in temp_list1)
- {
- result.Add(temp_firstelement + sm + s);
- }
- }
- List<string> temp_samplist2 = new List<string>();
- temp_samplist2.AddRange(reds);
- if (temp_samplist2.Count > 0)
- {
- List<string> temp_list2 = GetCombination(temp_samplist2, m, sm);
- result.AddRange(temp_list2);
- }
- return result;
- }
- #endregion
- #region 较通用的组选形态过滤
-
-
-
-
-
-
- public static List<int> ZxFilter(string str, int zx, List<int> list)
- {
- List<int> temp = new List<int>();
- 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<int> temp2 = new List<int>();
- for (int i = 0; i < temp.Count; i++)
- {
- List<int> 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
-
-
-
-
-
-
- public static int GetSameNumTimes(List<int> list1, List<int> list2)
- {
- return list1.Intersect(list2).ToList().Count;
- }
-
-
-
-
-
-
- public static List<string> Get012List(string xt)
- {
- List<string> temp = new List<string>();
- List<string> list = new List<string>();
- 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<string> GetZhList(string xt)
- {
- List<string> list = new List<string>();
- List<string> temp = new List<string>();
- 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<string> GetJoList(string xt)
- {
- List<string> list = new List<string>();
- List<string> temp = new List<string>();
- 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<string> GetDxList(string xt)
- {
- List<string> list = new List<string>();
- List<string> temp = new List<string>();
- 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<int> GetZxNumIntList(string num)
- {
- List<int> nums = GetNumList(num);
- List<int> zx = new List<int>();
- 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<string> GetZxNumList(string num)
- {
- List<int> nums = GetNumList(num);
- List<string> zx = new List<string>();
- 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;
- }
-
-
-
-
-
-
- public static List<string> GetNumStringList(string num)
- {
- List<string> nums = new List<string>();
- 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;
- }
-
-
-
-
-
-
- public static List<int> GetNumList(string num)
- {
- List<int> nums = new List<int>();
- 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<int>();
- }
- return nums;
- }
-
-
-
-
-
- public static List<int> GetGd11x5NumList(string num)
- {
- List<int> nums = new List<int>();
- string[] strs = num.Split(',');
- for (int i = 0; i < strs.Length; i++)
- {
- nums.Add(TypeConverter.ObjectToInt(strs[i], 0));
- }
- return nums;
- }
-
-
-
-
-
-
-
-
- public static List<int> GetNewEmHC(string num, int type,int anhc = 0)
- {
- List<int> nums = GetNumList(num);
- List<int> hc = new List<int>();
- int h1, h2, h3;
- List<int> val = new List<int>();
- 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;
- }
-
-
-
-
-
-
- public static List<int> GetEmHC(string num, int type)
- {
- List<int> nums = GetNumList(num);
- List<int> hc = new List<int>();
- 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;
- }
-
-
-
-
-
- public static string Get012Xt(string num)
- {
- StringBuilder xt = new StringBuilder();
- List<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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();
- }
-
-
-
-
-
- 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<int> 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值计算
-
-
-
-
-
- public static int GetAC(string[] kjh)
- {
- List<string> 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<string> GetCombination(string[] data, int count)
- {
- Dictionary<string, int> dic = new Dictionary<string, int>();
- List<string> output = new List<string>();
- 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<string, int> dd, string[] data, int count, int times, ref List<string> output)
- {
- Dictionary<string, int> dic = new Dictionary<string, int>();
- foreach (KeyValuePair<string, int> 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
-
- }
- }
|