将n个数按输入时顺序的逆序排列,用函数实现
解题思路: 定义两个指针,一个指向数组头部,一个指向数组尾部,头部每次+1,尾部每次-1;则在头部小于尾部的情况下进行数据交换即可。
答案:
#include<stdio.h>
#include<math.h>
void reorder(int *arry, int n)
{
int *start = arry;
int *end = arry + n - 1;
for (; start < end; start++, end--) {
int tmp = *start;
*start = *end;
*end = tmp;
}
return;
}
int main()
{
int arry[10];
printf("Please enter ten numbers:");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &arry[i]);
}
reorder(arry, 10);
printf("
");
for (int i = 0; i < 10; i++) {
printf("%d ", arry[i]);
}
printf("
");
system("pause");
return 0;
}