C#迷你猜數實例剖析。本站提示廣大學習愛好者:(C#迷你猜數實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是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#法式設計有所贊助。