左旋字符k个--旋2个 abcd--cdab
暴力求解法
#include
void left_move1(char arr[], int x, int y)
{
int i;
for (i = 0; i < x; i++)
{
int a = arr[0];
int j;
for (j = 0; j < y - 1; j++)
{
arr[j] = arr[j + 1];
}
arr[y-1] = a;
}
printf("%s",arr);
}
int main()
{
int k;
scanf("%d",&k); //左旋几个字符
char arr[]="abcdef";
int sz = sizeof(arr) / sizeof(arr[0])-1;
left_move1(arr,k,sz);
return 0;
}
三步旋转法
#include
#include
void reverse(char* a, char* b)
{
while (a
数组奇偶排列
#include
void print(int* a, int x)
{
int i;
for (i =0; i < x; i++)
{
printf("%d ",*(a+i));
}
}
void move(int a[], int x)
{
int* left = a;
int* right = &a[x - 1];
while (left < right)
{
while (left < right && (*left) % 2 == 1)
{
left++;
}
while (left < right && (*right) % 2 == 0)
{
right--;
}
if (left < right)
{
int x =* left;
*left = *right;
*right = x;
}
left++;
right --;
}
}
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int sz = sizeof(arr) / sizeof(arr[0]);
printf("%d\n",sz);
move(arr,sz);
print(arr, sz);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)