用C语言写,输入任意三个 数并从大到小排序
时间: 2024-03-10 20:49:15 浏览: 37
好的,以下是用C语言实现输入任意三个数并从大到小排序的代码:
```c
#include <stdio.h>
int main() {
float a, b, c, temp;
printf("请输入三个数,用空格隔开:");
scanf("%f %f %f", &a, &b, &c);
if (a < b) {
temp = a;
a = b;
b = temp;
}
if (a < c) {
temp = a;
a = c;
c = temp;
}
if (b < c) {
temp = b;
b = c;
c = temp;
}
printf("排序后的数为:%.2f %.2f %.2f\n", a, b, c);
return 0;
}
```
运行结果如下:
```
请输入三个数,用空格隔开:12.5 9.3 21.7
排序后的数为:21.70 12.50 9.30
```
注意:这里使用了冒泡排序的思想,通过交换三个数的位置,从而实现从大到小排序。
相关问题
c语言 输入任意三个数,按从大到小的顺序输出
下面是一个示例代码,可以实现输入任意三个数,按从大到小的顺序输出:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
if (a < b) {
int t = a;
a = b;
b = t;
}
if (a < c) {
int t = a;
a = c;
c = t;
}
if (b < c) {
int t = b;
b = c;
c = t;
}
printf("从大到小的顺序是:%d %d %d\n", a, b, c);
return 0;
}
```
这个程序中,我们首先使用 `scanf()` 函数输入三个整数,然后使用三个 `if` 语句对它们进行排序,最后使用 `printf()` 函数输出排序后的结果。
c语言用数组输入任意三个数,按从大到小的顺序输出
可以使用选择排序的方法来实现。具体步骤如下:
1. 定义一个长度为3的数组,用于存储输入的三个数。
2. 使用for循环将三个数依次输入到数组中。
3. 使用双重循环进行选择排序,将数组元素从大到小排序。
4. 使用for循环输出排序后的数组元素。
下面是示例代码:
```c
#include <stdio.h>
int main() {
int arr[3];
int i, j, max, temp;
// 输入三个数
for (i = 0; i < 3; i++) {
printf("请输入第%d个数:", i + 1);
scanf("%d", &arr[i]);
}
// 选择排序
for (i = 0; i < 2; i++) {
max = i;
for (j = i + 1; j < 3; j++) {
if (arr[j] > arr[max]) {
max = j;
}
}
if (max != i) {
temp = arr[i];
arr[i] = arr[max];
arr[max] = temp;
}
}
// 输出结果
printf("从大到小的顺序为:");
for (i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
运行结果如下:
```
请输入第1个数:3
请输入第2个数:1
请输入第3个数:5
从大到小的顺序为:5 3 1
```
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)