即abc输出abc,acb,bac,bca,cab,cba
思路:先以非递归方式,完成部分
public static void Rotate(char[] str){ var length = str.Length; int i = 1; for (int j = 0; j < length; j++) { foreach (var item in str) { Console.Write(item); } Console.WriteLine(); char temp = str[0]; //move left for (i = 1; i < length; i++) { str[i - 1] = str[i]; } str[i - 1] = temp; }}
得到结果是abc,bca,cab
每个数右边的n-1个数可以进行排列abc,acb.实际上就是对bc进行Rotate.
递归退出点就是输出点
public static void Rotate(char[] str,int pos){ var length = str.Length; if (pos == (length - 1)) { foreach (var item in str) { Console.Write(item); } Console.WriteLine(); return; } int i = 1; for (int j = pos; j < length; j++) { Rotate(str, pos+1); char temp = str[pos]; for (i = pos + 1; i < length; i++) { str[i - 1] = str[i]; } str[i - 1] = temp; }}
未完