c语言编程题:将一个数组从二分之一处切开,然后将中间的元素向两头搬移,同时两头元素向中间移,移4次

如:1 2 3 4 5 88 99 6 7 8 9 10,移动了四次之后是:3 4 5 88 1 2 9 10 99 6 7 8

#include <stdio.h>
#define N 12
void main()
{
int i, temp;
int times = 0;
int arr[N] = {1, 2, 3, 4, 5, 88, 99, 6, 7, 8, 9, 10};
do
{
temp = arr[(N-2)/2];
for(i=(N-2)/2;i>0;i--)
arr[i] = arr[i-1];
arr[0] = temp;
temp = arr[N/2];
for(i=N/2;i<N-1;i++)
arr[i] = arr[i+1];
arr[N-1] = temp;
times++;
}while(times <= 3);
printf("移动4次后的数组是:\n");
for(i=0;i<N;i++)
printf("%d ",arr[i]);
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-01-08
void process(int a[], int n) // a是数组,n是数组长度
{
int mid = n / 2;
int i, count;
for (count = 0; count < 4; count++) // 移动四次
{
int tmp = a[mid - 1];
for (i = mid - 1; i > 0; i--)
{
a[i] = a[i - 1];
}
a[i] = tmp;

tmp = a[mid];
for (i = mid; i < n - 1; i++)
{
a[i] = a[i + 1];
}
a[i] = tmp;
}
}

int main(void)
{
int a[] = {1, 2, 3, 4, 5, 88, 99, 6, 7, 8, 9, 10};
process(a, 12);
int i = 0;
while (i < 12) printf("%d ", a[i++]);
return 0;
}
相似回答