#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <string.h>
void reserve(char *left, char*right)
{
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void left_reserve(char arr[], int k)
{
int len = strlen(arr) - 1;
char *pstart = &arr[0];
char *pend = arr + len;
reserve(pstart, pstart + k - 1);
reserve(pstart + k, pend);
reserve(pstart, pend);
}
void right_reserve(char arr[], int k)
{
int len = strlen(arr) - 1;
char *pstart = &arr[0];
char *pend = arr + len;
reserve(pstart, pstart + len - k);
reserve(pend - k+1, pend);
reserve(pstart, pend);
}
int main()
{
char arr[10] = "AABCD";
char arr2[10] = "ABCDA";
char *p = arr;
int k = 5;
while (k)
{
left_reserve(arr, k);
if (strcmp(arr, arr2) == 0)
{
printf("1\n");
break;
}
right_reserve(p, k);
if (strcmp(p, arr2) == 0)
{
printf("1\n");
break;
}
k--;
}
if (0 == k)
{
printf("0\n");
}
system("pause");
}
转载于:https://blog.51cto.com/10740329/1709401