输入4个整数,要求从大到小排列,用排序方法写出代码
时间: 2023-05-26 07:02:44 浏览: 40
可以使用冒泡排序来完成:
```python
lst = list(map(int, input().split()))
for i in range(len(lst)-1):
for j in range(len(lst)-i-1):
if lst[j] < lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
print(lst)
```
示例输入:
```
10 5 7 3
```
示例输出:
```
[10, 7, 5, 3]
```
相关问题
输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求: 1.先输出其中的奇数,并按从大到小排列; 2.然后输出其中的偶数,并按从小到大排列,用c语言写出这段代码
```c
#include <stdio.h>
int main() {
int nums[10];
int odd[10], even[10]; // 定义奇数和偶数数组
int oddCount = 0, evenCount = 0; // 奇数和偶数个数
int i, j, temp;
// 输入10个整数
for (i = 0; i < 10; i++) {
scanf("%d", &nums[i]);
if (nums[i] % 2 == 0) { // 分别存入奇数和偶数数组
even[evenCount++] = nums[i];
} else {
odd[oddCount++] = nums[i];
}
}
// 奇数从大到小排序
for (i = 0; i < oddCount - 1; i++) {
for (j = 0; j < oddCount - i - 1; j++) {
if (odd[j] < odd[j + 1]) {
temp = odd[j];
odd[j] = odd[j + 1];
odd[j + 1] = temp;
}
}
}
// 偶数从小到大排序
for (i = 0; i < evenCount - 1; i++) {
for (j = 0; j < evenCount - i - 1; j++) {
if (even[j] > even[j + 1]) {
temp = even[j];
even[j] = even[j + 1];
even[j + 1] = temp;
}
}
}
// 输出排序后的结果
for (i = 0; i < oddCount; i++) {
printf("%d ", odd[i]);
}
for (i = 0; i < evenCount; i++) {
printf("%d ", even[i]);
}
return 0;
}
```
写出完整的程序 实现c语言字符串拆分出连续的整数,并由小到大排列
以下是一个示例程序,可以实现这个功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare_integers(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}
int main() {
char input_string[1000];
printf("Please enter a string of integers separated by non-digit characters:\n");
scanf("%s", input_string);
int numbers[1000];
int num_count = 0;
char* token = strtok(input_string, " ,.-");
while (token != NULL) {
int num = atoi(token);
if (num != 0) {
numbers[num_count] = num;
num_count++;
}
token = strtok(NULL, " ,.-");
}
qsort(numbers, num_count, sizeof(int), compare_integers);
printf("Sorted numbers:\n");
for (int i = 0; i < num_count; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
```
这个程序首先要求用户输入一个字符串(假设在运行程序时输入,而不是在代码中写死),然后使用 `strtok` 函数拆分出其中的整数。这个函数会将字符串分割成若干个子字符串,每个子字符串被非数字字符隔开。我们使用空格、逗号、短横线和句点作为分隔符。分割出的子字符串中,如果是数字,就将其转换成整数并存储到一个数组中。最后,我们使用 `qsort` 函数对这个数组进行排序,并将结果输出到屏幕上。
值得注意的是,为了避免不必要的麻烦,我们使用 `atoi` 函数将拆分出的每个字符串转换成整数,而不是使用 `sscanf` 函数。因为 `sscanf` 函数可能会受到用户输入格式的限制,而 `atoi` 函数则比较容错。当然,这个程序还可以有很多改进的空间,比如可以增加对用户输入非法的处理程序等等。
相关推荐
![](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)