/// <summary>
/// 字符左移b位
/// </summary>
/// <param name="b"></param>
public static void MoveBit(int b)
{
//产生字符数组。
char[] cs = new char[8];
for (int i = 0; i < 8; i++)
{
cs[i]=(char)(65+i);
}
//临时存储数组
char[] temp = new char[8];
for (int i = b; i < 8; i++)
{
var t = cs[i - b];
temp[i - b] = t;
cs[i - b] = cs[i];
}
for (int i = 8-b,j=0; i < 8; i++,j++)
{
cs[i]=temp[j];
}
string s = new string(cs);
MessageBox.Show(s);
}
/// <summary>
/// 翻转字符串
/// </summary>
/// <param name="cs"></param>
/// <param name="s">起始位置</param>
/// <param name="e">结束位置</param>
public static void Reverse(char[] cs, int s, int e)
{
int m = (s + e) / 2;
char temp;
for (int i = s, j = e; i <= m; i++, j--)
{
temp = cs[i];
cs[i] = cs[j];
cs[j] = temp;
}
}
/// <summary>
/// 通过翻转实现移位,o(n)。不需要临时存储空间。
/// </summary>
/// <param name="s"></param>
/// <param name="n"></param>
public static void MoveBit(char[] s, int n)
{
Reverse(s, 0, n-1);
Reverse(s, n, s.Length-1);
Reverse(s, 0, s.Length - 1);
}