C语言求1,3,6,4,8,9,10的逆序排列。 逆序10,9,8,4,6,3,1
时间: 2024-05-10 17:16:16 浏览: 13
可以使用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
求1,3,6,4,8,9,10的逆序排列。C语言
可以使用冒泡排序的思路来解决这个问题。具体实现代码如下:
```c
#include <stdio.h>
int main() {
int arr[] = {1, 3, 6, 4, 8, 9, 10};
int len = sizeof(arr) / sizeof(int);
int i, j, temp, count = 0;
// 冒泡排序
for (i = 0; i < len; i++) {
for (j = 0; j < len - i - 1; j++) {
if (arr[j] < arr[j+1]) { // 如果前面的数比后面的数小,则交换位置
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
count++; // 记录交换次数
}
}
}
// 输出逆序排列
printf("逆序排列是:");
for (i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("逆序排列中共有 %d 对逆序组合\n", count);
return 0;
}
```
输出结果为:
```
逆序排列是:10 9 8 6 4 3 1
逆序排列中共有 13 对逆序组合
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)