本题要求将给定的n个整数从大到小排序后输出。 输入 输入第一行给出一个不超过10的正整数n。第二行给出n个整数,其间以空格分隔。 输出 在一行中输出从大到小有序的数列,相邻数字间有一个空格,行末不得有多余空格。 用c语言实现
时间: 2023-11-14 14:09:35 浏览: 56
以下是用C语言实现的代码:
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
使用冒泡排序来进行排序,时间复杂度为O(n^2)。
相关问题
本题要求将给定的n个整数从大到小排序后输出。 输入格式: 输入第一行给出一个不超
过1000的正整数n,随后一行给出n个整数,数字间以空格分隔。
要求输出n个整数从大到小排序后的结果,数字间以空格分隔,但行末不得有空格。输入输出样例
输入样例:
4
5 -3 32 1
输出样例:
32 5 1 -3
要解决这个问题,我们可以使用python内置的sort方法,将输入的n个整数进行排序。首先读入第一行的正整数n,然后再读入一行n个整数。将这n个整数转换为一个列表,然后对这个列表调用sort方法,按降序排列。最后,将列表转换为字符串,将列表中的每个整数加上空格,然后将其输出。
代码如下:
```python
n = int(input())
nums = list(map(int, input().split()))
nums.sort(reverse=True)
output = ''
for num in nums:
output += str(num) + ' '
print(output[:-1])
```
本题要求将给定的n个整数从大到小排序后输出。\n\n输入格式:\n输入第一行给出一个不超过10的正整数n。第二行给出n个整数
这个题目要求将给定的n个整数从大到小排序后输出。
输入格式:
输入第一行给出一个不超过10的正整数n,第二行给出n个整数。
输出格式:
在一行内按非递增顺序输出排序后的整数,数字间以空格分隔,但行末不得有多余空格。