#include <stdio.h>
void move(int [20],int,int);
int main(void)
{
int numbers[20];
int i,n,m;
printf("how many numbers:");
scanf("%d",&n);
printf("input %d numbers\n",n);
for(i = 0; i < n; i++)
{
scanf("%d",&numbers[i]);
}
printf("how many place you want to move:");
scanf("%d",&m);
move(numbers,n,m);
printf("Now,they are:\n");
for(i = 0; i < n;i++)
printf("%d\t",numbers[i]);
printf("\n");
return 0;
}
void move(int array[20], int n, int m)
{
int *p;
int array_end;
array_end = *(array+n-1); //指向末尾元素
for(p = array+n-1;p > array; p--)
*p = *(p-1);
*array = array_end; //把末尾元素赋值给第一个
m--;
if(m > 0) //递归进行下一个元素移位
{
move(array,n,m);
}
}
有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数
最新推荐文章于 2022-11-26 22:04:54 发布