回复 4楼 wp231957
瞎写的,没有检查特殊情况
程序代码:
#include <stdio.h>
void shift_left( int* s, size_t n, size_t left )
{
if( n==0 || left%n==0 )
return;
left %= n;
// 求 n 和 left 的最大公约数gcd
size_t gcd = n;
for( size_t factor=left; factor; )
{
size_t tmp = gcd % factor;
gcd = factor;
factor = tmp;
}
// 循环gcd次
for( size_t i=0; i!=gcd; ++i )
{
int tmp = s[i];
for( size_t j=i; (j+left)%n!=i; j=(j+left)%n )
s[j] = s[(j+left)%n];
s[(i+n-left)%n] = tmp;
}
return;
}
int main( void )
{
int a[] = { 0,1,2,3, 4,5,6,7,8,9 };
shift_left( a, 10, 4 );
for( size_t i=0; i!=sizeof(a)/sizeof(a[0]); ++i )
printf( " %d", a[i] );
return 0;
}