编程之美上面提供了很好的思路,那么我就用代码实现,我这个代码实现可以循环左移和循环右移
// Perverse.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
void Reverse(char a[],int begin,int end)
{
for (;begin<end;begin++,end--)
{
char temp=a[end];
a[end]=a[begin];
a[begin]=temp;
}
}
void RightShift(char *array,int N,int K)
{
K%=N;
Reverse(array,0,N-K-1);
Reverse(array,N-K,N-1);
Reverse(array,0,N-1);
}
void LeftShift(char* array,int N,int K)
{
K%=N;
Reverse(array,K,N-1);
Reverse(array,0,K-1);
Reverse(array,0,N-1);
}
int _tmain(int argc, _TCHAR* argv[])
{
char a[]="abcd1234";
/*Reverse(a,1,2);
for (int i=0;i<5;i++)
{
printf("%3c ",a[i]);
}
system("pause");*/
LeftShift(a,8,2);
for (int i=0;i<8;i++)
{
printf("%c",a[i]);
}
system("pause");
return 0;
}