C#迷你猜数实例分析
程序员文章站
2023-12-12 14:55:34
本文实例讲述了c#迷你猜数。分享给大家供大家参考。具体如下:
using system;
using system.collections.generic;...
本文实例讲述了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#程序设计有所帮助。