using System;using System.Collections.Generic;using System.Text;namespace 暴力算法{ class Program { static void Main(string[] args) { int maxLength = 5; //设置可能最长的密码长度 Password.CrackPass(maxLength); } } class Password { //密码可能会包含的字符集合 //static char[] charSource = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', // 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; static char[] charSource = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; static int sLength = charSource.Length; //字符集长度 //得到密码长度从 1到maxLength的所有不同长的密码集合 public static void CrackPass(int maxLength) { for (int i = 1; i <= maxLength; i++) { char[] list = new char[i]; Crack(list, i); } } //得到长度为len所有的密码组合,在字符集charSource中 //递归表达式:fn(n)=fn(n-1)*sLenght; 大致是这个意思吧 private static void Crack(char[] list, int len) { if (len == 0) { //递归出口,list char[] 转换为字符串,并打印 Console.WriteLine(ArrayToString(list)); } else { for (int i = 0; i < sLength; i++) { list[len - 1] = charSource[i]; Crack(list, len - 1); } } } //list char[] 转换为字符串 private static String ArrayToString(char[] list) { if (list == null || list.Length == 0) return ""; StringBuilder buider = new StringBuilder(list.Length * 2); for (int i = 0; i < list.Length; i++) { buider.Append(list[i]); } return buider.ToString(); } }}