C语言求1,3,6,4,8,9,10的逆序排列。 逆序10,9,8,4,6,3,1
时间: 2024-05-10 08:16:16 浏览: 76
可以使用C语言的数组和循环来实现逆序排列,以下是实现代码:
```c
#include <stdio.h>
int main() {
int arr[] = {1, 3, 6, 4, 8, 9, 10};
int len = sizeof(arr) / sizeof(arr[0]);
// 交换数组元素,实现逆序排列
for (int i = 0; i < len / 2; i++) {
int temp = arr[i];
arr[i] = arr[len - 1 - i];
arr[len - 1 - i] = temp;
}
// 输出逆序排列后的数组
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
输出结果为:`10 9 8 4 6 3 1`
相关问题
c语言求1,3,6,4,8,9,10的逆序排列。
逆序排列就是将原序列中的元素倒序排列,例如原序列为1,2,3,逆序排列后为3,2,1。
下面是C语言代码实现1,3,6,4,8,9,10的逆序排列:
```c
#include <stdio.h>
int main() {
int arr[] = {1, 3, 6, 4, 8, 9, 10};
int len = sizeof(arr) / sizeof(int);
// 逆序排列
for (int i = 0; i < len / 2; i++) {
int temp = arr[i];
arr[i] = arr[len - i - 1];
arr[len - i - 1] = temp;
}
// 输出结果
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
输出结果为:10 9 8 4 6 3 1
请用c语言求1,3,6,4,8,9,10的逆序排列。
以下是使用C语言实现的逆序排列:
```c
#include <stdio.h>
int main() {
int arr[] = {1, 3, 6, 4, 8, 9, 10};
int n = sizeof(arr) / sizeof(int);
printf("Original array: ");
for(int i=0; i<n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
for(int i=0, j=n-1; i<j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
printf("Reversed array: ");
for(int i=0; i<n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
Original array: 1 3 6 4 8 9 10
Reversed array: 10 9 8 4 6 3 1
```
阅读全文
相关推荐
















