以前的老代码翻出来看,还是喜欢以前写的,精练些。
static void Main(string[] args) { int[] a = { 32, 324, 645, 856, 3, 56, 8, 6, 4, 368, 87, 55, 78, 434, 12, 345, 787, 90, 123, 304, 90 }; int bit = 3; Foo(a, bit); for (int i = 0; i < a.Length; i++) { Console.Write("{0} ", a[i]); } Console.Read(); } private static void Foo(int[] a, int bit) { List [] t = new List [10]; for (int i = 0; i < 10; i++) { t[i] = new List (); } while (bit > 0) { for (int i = 0; i < a.Length; i++) { //这里比较重要, 数%10^x/10^(x-1) t[a[i] % ((int)Math.Pow(10, 4 - bit)) / ((int)Math.Pow(10, (3 - bit)))].Add(a[i]); } Proc(t, a); bit--; } } static void Proc(List [] t, int[] a) { int x = 0; for (int i = 0; i < t.Length; i++) { foreach (int l in t[i]) { a[x++] = l; } } for (int i = 0; i < 10; i++) { t[i] = new List (); } }