C#迷你猜数实例分析
本文实例讲述了C#迷你猜数。分享给大家供大家参考。具体如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 迷你猜数 { class Program { static void Main(string[] args) { List<int> LisData = GetData(); LisData = FilterData(LisData, 4659, 0, 1); LisData = FilterData(LisData, 8763, 0, 1); LisData = FilterData(LisData, 1035, 0, 1); LisData = FilterData(LisData, 3249, 1, 1); LisData = FilterData(LisData, 0497, 0, 2); LisData = FilterData(LisData, 9148, 0, 2); foreach(var num in LisData) { Console.WriteLine(num); } Console.ReadKey(); } /// <summary> /// 获取所有可用数据 /// </summary> /// <returns></returns> private static List<int> GetData() { List<int> LisData = new List<int>(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { for (int m = 0; m < 10; m++) { if((i!=j) && (i!=k) && (i!=m) && (j!=k) && (j!=m) && (k!=m)) { LisData.Add(i * 1000 + j * 100 + k * 10 + m); } } } } } return LisData; } private static List<int> FilterData(List<int> LisData,int Num,int a,int b) { List<int> lisDataNum=new List<int>(); int[] LisNum = new int[4]; //将Num拆分成一个数组 int[] Numnum = SplitNum(Num); foreach(var Data in LisData) { int aNum = 0; int bNum = 0; //将Data拆分成一个数组 LisNum = SplitNum(Data); #region 求a,b的个数 for (int i=0;i<4;i++) { //求a的个数 if(Numnum[i]==LisNum[i]) { aNum++; } //求b的个数 for(int j=0;j<4;j++) { if(Numnum[i]==LisNum[j]) { bNum++; } } } bNum -= aNum; #endregion 求a,b的个数 //判断a,b if((aNum==a)&&(bNum==b)) { lisDataNum.Add(Data); } } return lisDataNum; } /// <summary> /// 拆分四位数 /// </summary> /// <param name="Num"></param> /// <returns></returns> private static int[] SplitNum(int Num) { int[] Numnu = new int[4]; for (int i = 0; i < 4; i++) { Numnu[i] = Num % 10; Num = Num / 10; } return Numnu; } } }
希望本文所述对大家的C#程序设计有所帮助。
赞 (0)