#include<stdio.h>
void input(int *,int );
void swap(int *,int ,int);
void output(int *,int);
int main()
{
int m,n;
printf("How many numbers do you want ? ");
scanf("%d",&n);
int num[n];
printf("enter %d numbers: \n",n);
input(num,n);
printf("How many numbers do you want to move? (It must less than %d) ",n);
scanf("%d",&m);
while(m>=n)
{
printf("input error!\n");
scanf("%d",&m);
}
swap(num,0,n-1);
swap(num,0,n-m-1);
swap(num,n-m,n-1);
printf("\nthe result of moving numvers is :\n");
output(num,n);
return 0;
}
void input(int *a,int n)
{
int *head=a;
while(a<=head+n-1)
scanf("%d",a++);
}
void swap(int *a,int i,int j)
{
int temp;
for(;i<=(i+j)/2;i++,j--)/*这个公式适用于所有需要中间元素参与的题*/
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
void output(int *a,int n)
{
int *head=a;
while(a<=head+n-1)
printf("%d ",*(a++));
}
将数组元素循环流动的问题(即数组第一个元素变成最后一个,后面的补上,如此循环几次)
最新推荐文章于 2022-06-27 16:02:26 发布