第五次上机作业:(请用指针实现)
1. 数据移位
有n(n<=10)个整数,使前面各数顺序向后移m(m<=n)个位置,最后m个数变成最前面的m个数,如图所示。编写函数shift(a,n,m)实现上述功能,在主函数中输入n个整数和输出调整后的n个数。
SAMPLE OUTPUT nInput n(n<=10):10
Input 10 integers:1 2 3 4 5 6 7 8 9 10
Output the original:1 2 3 4 5 6 7 8 9 10
Input m(m<=10):4
Output the shifted:7 8 9 10 1 2 3 4 5 6
#include
#define N 10
void shift(int a[ ],int n,int m);
void main( )
{ int i,n,m;
int a[N];
printf("Input n(n<=10):");
scanf("%d",&n);
printf("Input %d integers:\n",n);
for(i=0;i
scanf("%d",a+i);
printf("Output the original:\n");
for(i=0;i
printf("%d ",*(a+i));
printf("\n");
printf("Input m(m<=%d):",n);
scanf("%d",&m);
shift(a,n,m);
printf("Output the shifted:\n");
for(i=0;i
printf("%d ",*(a+i));
printf("\n");
}
void shift(int a[ ],int n,int m)
{ int i,j,t;
for(i=0;i
{ t=a[n-1];
for(j=n-2;j>=0;j--)
a[j+1]=a[j];
a[0]=t;
}
}