字符串有 n 个字符下,无论需要左旋 m 次,其实只需旋转 m%n 次.
void leftspin(char* ch, int n)
{
int m = strlen(ch);//计算长度
int n = n % m;//实际次数
for (int i = 0; i < n; i++)
{
char tmp = ch[0];
int j = 0;
for (j = 0; j < len - 1; j++)
{
ch[j] = ch[j + 1];
}
ch[j] = tmp;
}
}
int main()
{
char ch[] = "hellolz";
int n = 0;
scanf("%d", &n);
leftspin(ch, n);
puts(ch);
return 0;
}
谢谢观看!