LotteryUtils.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using SCC.Models;
  7. namespace SCC.Crawler.DT
  8. {
  9. /// <summary>
  10. /// 彩票相关术语计算
  11. /// </summary>
  12. public class LotteryUtils
  13. {
  14. /// <summary>
  15. /// 计算跨度;
  16. /// count:从左至右依次取号码个数;
  17. /// </summary>
  18. /// <param name="OpenCode">号码数组</param>
  19. /// <param name="count">从左至右依次取号码个数</param>
  20. /// <returns></returns>
  21. public static int GetSpan(IList<int> OpenCode, int count = 0)
  22. {
  23. List<int> list = new List<int>(OpenCode);
  24. int n = 0 >= count ? list.Count : count;
  25. n = n > list.Count ? list.Count - 1 : n - 1;
  26. for (int i = list.Count - 1; i > n; i--)
  27. { list.RemoveAt(i); }
  28. list.Sort();
  29. return Math.Abs(list[0] - list[list.Count - 1]);
  30. }
  31. /// <summary>
  32. /// 计算和值;
  33. /// count:从左至右依次取号码个数;
  34. /// </summary>
  35. /// <param name="OpenCode">号码数组</param>
  36. /// <param name="count">从左至右依次取号码个数</param>
  37. /// <returns></returns>
  38. public static int GetSum(IList<int> OpenCode, int count = 0)
  39. {
  40. List<int> list = new List<int>(OpenCode);
  41. int n = 0 >= count ? list.Count : count;
  42. n = n > list.Count ? list.Count - 1 : n - 1;
  43. for (int i = list.Count - 1; i > n; i--)
  44. { list.RemoveAt(i); }
  45. int sum = 0;
  46. foreach (int item in list)
  47. { sum = sum + item; }
  48. return sum;
  49. }
  50. /// <summary>
  51. /// 计算大小比例;
  52. /// 大于等于splitNumber算大数;
  53. /// count:从左至右依次取号码个数;
  54. /// </summary>
  55. /// <param name="OpenCode">号码数组</param>
  56. /// <param name="splitNumber">分隔数字</param>
  57. /// <param name="count">从左至右依次取号码个数</param>
  58. /// <returns></returns>
  59. public static string GetProportionOfDX(IList<int> OpenCode, int splitNumber, int count = 0)
  60. {
  61. List<int> list = new List<int>(OpenCode);
  62. int n = 0 >= count ? list.Count : count;
  63. n = n > list.Count ? list.Count - 1 : n - 1;
  64. for (int i = list.Count - 1; i > n; i--)
  65. { list.RemoveAt(i); }
  66. int a = 0, b = 0;
  67. foreach (int item in list)
  68. {
  69. if (item >= splitNumber)
  70. { a++; }
  71. else { b++; }
  72. }
  73. return a.ToString() + ":" + b.ToString();
  74. }
  75. /// <summary>
  76. /// 计算奇偶比例;
  77. /// count:从左至右依次取号码个数;
  78. /// </summary>
  79. /// <param name="OpenCode">号码数组</param>
  80. /// <param name="count">从左至右依次取号码个数</param>
  81. /// <returns></returns>
  82. public static string GetProportionOfJO(IList<int> OpenCode, int count = 0)
  83. {
  84. List<int> list = new List<int>(OpenCode);
  85. int n = 0 >= count ? list.Count : count;
  86. n = n > list.Count ? list.Count - 1 : n - 1;
  87. for (int i = list.Count - 1; i > n; i--)
  88. { list.RemoveAt(i); }
  89. int a = 0, b = 0;
  90. foreach (int item in list)
  91. {
  92. if (1 == item % 2)
  93. { a++; }
  94. else { b++; }
  95. }
  96. return a.ToString() + ":" + b.ToString();
  97. }
  98. /// <summary>
  99. /// 计算012比例
  100. /// </summary>
  101. /// <param name="OpenCode">号码数组</param>
  102. /// <returns></returns>
  103. public static string GetProportionOf012(IList<int> OpenCode)
  104. {
  105. int[] t = { 0, 0, 0 };
  106. foreach (var item in OpenCode)
  107. {
  108. t[item % 3]++;
  109. }
  110. return t[0].ToString() + ":" + t[1].ToString() + ":" + t[2].ToString();
  111. }
  112. /// <summary>
  113. /// 计算质合比例
  114. /// </summary>
  115. /// <param name="OpenCode"></param>
  116. /// <returns></returns>
  117. public static string GetProportionOfZh(IList<int> OpenCode)
  118. {
  119. int[] t = { 0, 0 };
  120. foreach (var item in OpenCode)
  121. {
  122. if (IsPrimeNumbers(item))
  123. t[0]++;
  124. else
  125. t[1]++;
  126. }
  127. return t[0].ToString() + ":" + t[1].ToString();
  128. }
  129. /// <summary>
  130. /// 判断是否为质数
  131. /// </summary>
  132. /// <param name="number"></param>
  133. /// <returns></returns>
  134. public static bool IsPrimeNumbers(int number)
  135. {
  136. if (0 == number)
  137. return false;
  138. int iii = number / 2;
  139. for (int ii = 2; ii <= iii; ii++)
  140. {
  141. if (0 == number % ii)
  142. {
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. /// <summary>
  149. /// 福彩3D判断大小
  150. /// </summary>
  151. /// <param name="item"></param>
  152. /// <returns></returns>
  153. public static bool IsDxNumbers(int item)
  154. {
  155. if (item > 4)
  156. {
  157. return true;
  158. }
  159. else
  160. {
  161. return false;
  162. }
  163. }
  164. /// <summary>
  165. /// 计算三区比(适用于双色球)
  166. /// </summary>
  167. /// <param name="item"></param>
  168. /// <returns></returns>
  169. public static string SsqSanQu(IList<int> kjh)
  170. {
  171. int sanqu = 0, sanqu1 = 0, sanqu2 = 0;
  172. foreach (int item in kjh)
  173. {
  174. if (item >= 1 && item <= 11)
  175. {
  176. sanqu++;
  177. }
  178. if (item >= 12 && item <= 22)
  179. {
  180. sanqu1++;
  181. }
  182. if (item >= 23 && item <= 33)
  183. {
  184. sanqu2++;
  185. }
  186. }
  187. return string.Format("{0}:{1}:{2}", sanqu, sanqu1, sanqu2);
  188. }
  189. /// <summary>
  190. /// 计算ac值
  191. /// </summary>
  192. /// <param name="kjh">需计算的开奖号码</param>
  193. /// <returns></returns>
  194. public static int GetAC(string[] kjh)
  195. {
  196. List<string> result = GetCombination(kjh, 2);
  197. ArrayList acarray = new ArrayList();
  198. int tpac = 0;
  199. for (int i = 0; i < result.Count; i++)
  200. {
  201. string[] tp = result[i].Split(',');
  202. int tmp = Math.Abs(Convert.ToInt32(tp[0]) - Convert.ToInt32(tp[1]));
  203. if (!acarray.Contains(tmp))
  204. {
  205. tpac++;
  206. acarray.Add(tmp);
  207. }
  208. }
  209. return tpac - (kjh.Length - 1);
  210. }
  211. public static List<string> GetCombination(string[] data, int count)
  212. {
  213. Dictionary<string, int> dic = new Dictionary<string, int>();
  214. List<string> output = new List<string>();
  215. for (int i = 0; i < data.Length; i++)
  216. {
  217. dic.Add(data[i], i);
  218. }
  219. SelectN(dic, data, count, 1, ref output);
  220. return output;
  221. }
  222. private static void SelectN(Dictionary<string, int> dd, string[] data, int count, int times, ref List<string> output)
  223. {
  224. Dictionary<string, int> dic = new Dictionary<string, int>();
  225. foreach (KeyValuePair<string, int> kv in dd)
  226. {
  227. for (int i = kv.Value + 1; i < data.Length; i++)
  228. {
  229. if (times < count - 1)
  230. {
  231. dic.Add(kv.Key + "," + data[i], i);
  232. }
  233. else
  234. {
  235. output.Add(kv.Key + "," + data[i]);
  236. }
  237. }
  238. }
  239. times++;
  240. if (dic.Count > 0)
  241. {
  242. SelectN(dic, data, count, times, ref output);
  243. }
  244. }
  245. /// <summary>
  246. /// 判断奇偶
  247. /// </summary>
  248. /// <param name="item"></param>
  249. /// <returns></returns>
  250. public static bool IsJoNumbers(int item)
  251. {
  252. if (item % 2 == 0)
  253. {
  254. return false;
  255. }
  256. else
  257. {
  258. return true;
  259. }
  260. }
  261. /// <summary>
  262. /// 根据开奖号获取奇偶形态
  263. /// </summary>
  264. /// <param name="list"></param>
  265. /// <returns></returns>
  266. public static string GetJOString(IList<int> list)
  267. {
  268. StringBuilder sb = new StringBuilder(list.Count * 2);
  269. foreach (var item in list)
  270. {
  271. if (item % 2 == 0)
  272. {
  273. sb.Append("偶");
  274. }
  275. else
  276. {
  277. sb.Append("奇");
  278. }
  279. }
  280. return sb.ToString();
  281. }
  282. /// <summary>
  283. /// 根据开奖号获取大小形态
  284. /// </summary>
  285. /// <param name="list"></param>
  286. /// <returns></returns>
  287. public static string GetDXString(IList<int> list, int splitNumber)
  288. {
  289. StringBuilder sb = new StringBuilder(list.Count * 2);
  290. foreach (var item in list)
  291. {
  292. if (item >= splitNumber)
  293. {
  294. sb.Append("大");
  295. }
  296. else
  297. {
  298. sb.Append("小");
  299. }
  300. }
  301. return sb.ToString();
  302. }
  303. /// <summary>
  304. /// 根据开奖号获取质合形态
  305. /// </summary>
  306. /// <param name="list"></param>
  307. /// <returns></returns>
  308. public static string GetZHString(IList<int> list)
  309. {
  310. StringBuilder sb = new StringBuilder(list.Count * 2);
  311. bool zh;
  312. foreach (var item in list)
  313. {
  314. zh = true;
  315. if (item == 0)
  316. {
  317. sb.Append("合");
  318. continue;
  319. }
  320. for (int i = 2; i < item; i++)
  321. {
  322. if (item % i == 0)
  323. {
  324. sb.Append("合");
  325. zh = false;
  326. break;
  327. }
  328. }
  329. if (zh)
  330. {
  331. sb.Append("质");
  332. }
  333. }
  334. return sb.ToString();
  335. }
  336. /// <summary>
  337. /// 根据开奖号获取012形态
  338. /// </summary>
  339. /// <param name="list"></param>
  340. /// <returns></returns>
  341. public static string Get012String(IList<int> list)
  342. {
  343. StringBuilder sb = new StringBuilder(list.Count * 2);
  344. foreach (var item in list)
  345. {
  346. sb.Append((item % 3).ToString());
  347. }
  348. return sb.ToString();
  349. }
  350. /// <summary>
  351. /// 根据开奖号获取和尾值形态
  352. /// </summary>
  353. /// <param name="list"></param>
  354. /// <returns></returns>
  355. public static string GetHWString(IList<int> list)
  356. {
  357. string sum = GetSum(list).ToString();
  358. return sum[sum.Length - 1].ToString();
  359. }
  360. /// <summary>
  361. /// 模拟开奖号期数下拉列表
  362. /// </summary>
  363. /// <param name="topSize"></param>
  364. /// <param name="latestQi"></param>
  365. /// <param name="lottery"></param>
  366. /// <param name="formartUrl">格式化期数</param>
  367. /// <returns></returns>
  368. public static string GetLotteryDDLQi(int topSize, long latestQi, string lottery, string formartUrl)
  369. {
  370. StringBuilder sp = new StringBuilder(512);
  371. int minQi = 0, y = 0, j = 1, endQi = 0, line = 1, _qi = 0;
  372. int year = (int)latestQi / 1000;
  373. int startQi = year * 1000 + 1;
  374. switch (lottery)
  375. {
  376. case "qlc":
  377. minQi = 2002001;
  378. break;
  379. case "3d":
  380. minQi = 2002001;
  381. break;
  382. case "ssq":
  383. minQi = 2003001;
  384. break;
  385. case "dlt":
  386. minQi = 2007001;
  387. break;
  388. case "p3":
  389. minQi = 2004001;
  390. break;
  391. case "p5":
  392. minQi = 2004001;
  393. break;
  394. case "qxc":
  395. minQi = 2006001;
  396. break;
  397. case "3dshijihao":
  398. minQi = 2002001;
  399. break;
  400. case "p3shijihao":
  401. minQi = 2004001;
  402. break;
  403. case "hc1":
  404. minQi = 2002001;
  405. break;
  406. }
  407. while (line <= topSize)
  408. {
  409. _qi = (int)latestQi - j;
  410. if (_qi < minQi)
  411. break;
  412. if (_qi < startQi)
  413. {
  414. y = y + 1;
  415. startQi = (year - y) * 1000 + 1;
  416. endQi = (year - y) * 1000 + 153;
  417. if (lottery == "3d" || lottery == "p3" || lottery == "p5" || lottery == "3dshijihao" || lottery == "p3shijihao" || lottery == "hc1")
  418. {
  419. endQi = (year - y) * 1000 + 358;
  420. if (2014 == (year - y))
  421. endQi = 2014357;
  422. }
  423. if (0 == ((year - y) % 4))
  424. {
  425. endQi = endQi + 1;
  426. }
  427. latestQi = endQi;
  428. _qi = endQi;
  429. j = 0;
  430. }
  431. sp.AppendFormat("<li><a href=\"{0}.htm\">{1}</a></li>", string.Format(formartUrl + _qi), _qi);
  432. j++;
  433. line++;
  434. }
  435. return sp.ToString();
  436. }
  437. /// <summary>
  438. /// 手机端开奖结果下拉
  439. /// </summary>
  440. /// <param name="topSize"></param>
  441. /// <param name="latestQi"></param>
  442. /// <returns></returns>
  443. public static string GetLotteryDDLQi(int topSize, long latestQi, string lottery)
  444. {
  445. StringBuilder sp = new StringBuilder(512);
  446. int minQi = 0, y = 0, j = 1, endQi = 0, line = 1, _qi = 0;
  447. int year = (int)latestQi / 1000;
  448. int startQi = year * 1000 + 1;
  449. switch (lottery)
  450. {
  451. case "qlc":
  452. minQi = 2002001;
  453. break;
  454. case "3d":
  455. minQi = 2002001;
  456. break;
  457. case "ssq":
  458. minQi = 2003001;
  459. break;
  460. case "dlt":
  461. minQi = 2007001;
  462. break;
  463. case "p3":
  464. minQi = 2004001;
  465. break;
  466. case "p5":
  467. minQi = 2004001;
  468. break;
  469. case "qxc":
  470. minQi = 2006001;
  471. break;
  472. case "3dshijihao":
  473. minQi = 2002001;
  474. break;
  475. case "p3shijihao":
  476. minQi = 2004001;
  477. break;
  478. case "hc1":
  479. minQi = 2002001;
  480. break;
  481. case "df6j1":
  482. minQi = 2016059;
  483. break;
  484. }
  485. while (line <= topSize)
  486. {
  487. _qi = (int)latestQi - j;
  488. if (_qi < minQi)
  489. break;
  490. if (_qi < startQi)
  491. {
  492. y = y + 1;
  493. startQi = (year - y) * 1000 + 1;
  494. endQi = (year - y) * 1000 + 153;
  495. if (lottery == "3d" || lottery == "p3" || lottery == "p5" || lottery == "3dshijihao" || lottery == "p3shijihao" || lottery == "hc1" || lottery == "df6j1")
  496. {
  497. endQi = (year - y) * 1000 + 358;
  498. if (2014 == (year - y))
  499. endQi = 2014357;
  500. }
  501. if (0 == ((year - y) % 4))
  502. {
  503. endQi = endQi + 1;
  504. }
  505. latestQi = endQi;
  506. _qi = endQi;
  507. j = 0;
  508. }
  509. sp.AppendFormat("<option>{0}</option>", _qi);
  510. j++;
  511. line++;
  512. }
  513. return sp.ToString();
  514. }
  515. /// <summary>
  516. /// 模拟开奖号、试机号、开机号期数下拉列表
  517. /// </summary>
  518. /// <param name="topSize"></param>
  519. /// <param name="latestQi"></param>
  520. /// <param name="lottery"></param>
  521. /// <param name="formatHTML"></param>
  522. /// <param name="formatUrl"></param>
  523. /// <returns></returns>
  524. public static string GetLotteryDDLQi(int topSize, long latestQi, string lottery, string formatHTML, string formatUrl)
  525. {
  526. var sp = new StringBuilder(topSize * (formatHTML.Length + formatUrl.Length + 10));
  527. int minQi = 0, y = 0, j = 0, endQi = 0, line = 1, _qi = 0;
  528. int year = (int)latestQi / 1000;
  529. int startQi = year * 1000 + 1;
  530. switch (lottery)
  531. {
  532. case "qlc":
  533. minQi = 2002001;
  534. break;
  535. case "3d":
  536. minQi = 2002001;
  537. break;
  538. case "ssq":
  539. minQi = 2003001;
  540. break;
  541. case "dlt":
  542. minQi = 2007001;
  543. break;
  544. case "p3":
  545. minQi = 2004001;
  546. break;
  547. case "p5":
  548. minQi = 2004001;
  549. break;
  550. case "qxc":
  551. minQi = 2006001;
  552. break;
  553. case "3dshijihao":
  554. minQi = 2002001;
  555. break;
  556. case "p3shijihao":
  557. minQi = 2004001;
  558. break;
  559. case "hc1":
  560. minQi = 2002001;
  561. break;
  562. }
  563. while (line <= topSize)
  564. {
  565. _qi = (int)latestQi - j;
  566. if (_qi < minQi)
  567. break;
  568. if (_qi < startQi)
  569. {
  570. y = y + 1;
  571. startQi = (year - y) * 1000 + 1;
  572. endQi = (year - y) * 1000 + 153;
  573. if (lottery == "3d" || lottery == "p3" || lottery == "p5" || lottery == "3dshijihao" || lottery == "p3shijihao" || lottery == "hc1")
  574. {
  575. endQi = (year - y) * 1000 + 358;
  576. if (2014 == (year - y))
  577. endQi = 2014357;
  578. }
  579. if (0 == ((year - y) % 4))
  580. {
  581. endQi = endQi + 1;
  582. }
  583. latestQi = endQi;
  584. _qi = endQi;
  585. j = 0;
  586. }
  587. sp.AppendFormat(formatHTML, string.Format(formatUrl, _qi), _qi);
  588. j++;
  589. line++;
  590. }
  591. return sp.ToString();
  592. }
  593. /// <summary>
  594. /// 获取开奖号码
  595. /// </summary>
  596. /// <typeparam name="TEntity"></typeparam>
  597. /// <param name="entity"></param>
  598. /// <param name="indexStart"></param>
  599. /// <param name="indexEnd"></param>
  600. /// <returns></returns>
  601. public static IList<int> GetOpenCodeList<TEntity>(TEntity entity, int indexStart, int indexEnd) where TEntity : LotteryOpenCode
  602. {
  603. IList<int> list = new List<int>(entity.OpenCode);
  604. if (-1 != indexEnd)
  605. {
  606. for (int i = list.Count - 1; i >= indexEnd; i--)
  607. { list.RemoveAt(i); }
  608. }
  609. for (int i = 0; i < indexStart; i++)
  610. { list.RemoveAt(0); }
  611. return list;
  612. }
  613. /// <summary>
  614. /// 福建31选7三区比
  615. /// </summary>
  616. /// <param name="list"></param>
  617. /// <returns></returns>
  618. public static string Fj31x7SanQu(IList<int> list)
  619. {
  620. int sanqu = 0, sanqu1 = 0, sanqu2 = 0;
  621. foreach (int item in list)
  622. {
  623. if (item >= 1 && item <= 10)
  624. {
  625. sanqu++;
  626. }
  627. if (item >= 11 && item <= 20)
  628. {
  629. sanqu1++;
  630. }
  631. if (item >= 21 && item <= 31)
  632. {
  633. sanqu2++;
  634. }
  635. }
  636. return string.Format("{0}:{1}:{2}", sanqu, sanqu1, sanqu2);
  637. }
  638. /// <summary>
  639. /// 福建31选7三区比
  640. /// </summary>
  641. /// <param name="list"></param>
  642. /// <returns></returns>
  643. public static string Fj36x7SanQu(IList<int> list)
  644. {
  645. int sanqu = 0, sanqu1 = 0, sanqu2 = 0;
  646. foreach (int item in list)
  647. {
  648. if (item >= 1 && item <= 12)
  649. {
  650. sanqu++;
  651. }
  652. if (item >= 13 && item <= 24)
  653. {
  654. sanqu1++;
  655. }
  656. if (item >= 25 && item <= 36)
  657. {
  658. sanqu2++;
  659. }
  660. }
  661. return string.Format("{0}:{1}:{2}", sanqu, sanqu1, sanqu2);
  662. }
  663. /// <summary>
  664. /// 华东15选5三区比
  665. /// </summary>
  666. /// <param name="list"></param>
  667. /// <returns></returns>
  668. public static string Hd15x5SanQu(IList<int> list)
  669. {
  670. int sanqu = 0, sanqu1 = 0, sanqu2 = 0;
  671. foreach (int item in list)
  672. {
  673. if (item >= 1 && item <= 5)
  674. {
  675. sanqu++;
  676. }
  677. if (item >= 6 && item <= 10)
  678. {
  679. sanqu1++;
  680. }
  681. if (item >= 11 && item <= 15)
  682. {
  683. sanqu2++;
  684. }
  685. }
  686. return string.Format("{0}:{1}:{2}", sanqu, sanqu1, sanqu2);
  687. }
  688. /// <summary>
  689. /// 南粤36选7三区比
  690. /// </summary>
  691. /// <param name="list"></param>
  692. /// <returns></returns>
  693. public static string Ny36x7SanQu(IList<int> list)
  694. {
  695. int sanqu = 0, sanqu1 = 0, sanqu2 = 0;
  696. foreach (int item in list)
  697. {
  698. if (item >= 1 && item <= 12)
  699. {
  700. sanqu++;
  701. }
  702. if (item >= 13 && item <= 24)
  703. {
  704. sanqu1++;
  705. }
  706. if (item >= 25 && item <= 36)
  707. {
  708. sanqu2++;
  709. }
  710. }
  711. return string.Format("{0}:{1}:{2}", sanqu, sanqu1, sanqu2);
  712. }
  713. }
  714. }