用c语言写一个可以将三个数由小到大顺序排列的函数
时间: 2023-11-27 13:51:10 浏览: 37
以下是用 C 语言编写的将三个数由小到大顺序排列的函数:
```c
void sort(int* a, int* b, int* c) {
int temp;
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;
}
}
```
这个函数接收三个指向整数的指针,按照从小到大的顺序将它们排列。使用时可以这样调用:
```c
int a = 3, b = 1, c = 2;
sort(&a, &b, &c);
printf("%d %d %d\n", a, b, c);
```
输出结果为:
```
1 2 3
```
相关问题
用c语言写一个可以将三个数由小到大顺序排列的函数,在主函数中输入三个数,按由小到大排列
以下是用C语言编写的,可以将三个数由小到大顺序排列的函数:
```c
#include <stdio.h>
void sort(int *a, int *b, int *c)
{
int temp;
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;
}
}
int main()
{
int a, b, c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
sort(&a, &b, &c);
printf("排序后: %d %d %d\n", a, b, c);
return 0;
}
```
在主函数中,我们首先输入三个整数,然后调用 `sort` 函数,该函数需要传入三个指针参数,表示三个数的地址。在函数内部,我们使用冒泡排序的思想,将三个数进行排序。最后,我们输出排序后的结果。
c语言写个程序,将三个数字从小到大排列
### 回答1:
下面是一个使用c语言实现将三个数字从小到大排列的简单程序:
```
#include <stdio.h>
int main()
{
int a, b, c, temp;
printf("请输入三个数字:");
scanf("%d%d%d", &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("从小到大排列为:%d %d %d\n", a, b, c);
return 0;
}
```
上面的代码通过使用临时变量和if语句进行交换实现了从小到大排列。
### 回答2:
以下是使用C语言编写的将三个数字从小到大排列的程序:
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sort(int *arr) {
if (arr[0] > arr[1])
swap(&arr[0], &arr[1]);
if (arr[1] > arr[2]) {
swap(&arr[1], &arr[2]);
if (arr[0] > arr[1])
swap(&arr[0], &arr[1]);
}
}
int main() {
int arr[3];
printf("请输入三个数字:\n");
scanf("%d %d %d", &arr[0], &arr[1], &arr[2]);
sort(arr);
printf("从小到大排列为:\n");
printf("%d %d %d\n", arr[0], arr[1], arr[2]);
return 0;
}
```
程序中使用了两个函数,`swap`函数用来交换两个数字的值,`sort`函数用来将数组中的数字从小到大排列。在`main`函数中,先获取用户输入的三个数字,然后调用`sort`函数对数组进行排序,最后将排序后的结果输出到屏幕上。
编译并运行该程序后,用户可以输入三个数字,程序将会按照从小到大的顺序输出这三个数字。例如,用户输入的数字为`3 1 2`,则程序输出`1 2 3`。
### 回答3:
下面是一个使用C语言编写的程序,可以将输入的三个数字按从小到大的顺序进行排列。
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sort(int *a, int *b, int *c) {
if (*a > *b) {
swap(a, b);
}
if (*b > *c) {
swap(b, c);
}
if (*a > *b) {
swap(a, b);
}
}
int main() {
int num1, num2, num3;
printf("请输入三个数字:");
scanf("%d %d %d", &num1, &num2, &num3);
sort(&num1, &num2, &num3);
printf("从小到大排序后的结果为:%d %d %d\n", num1, num2, num3);
return 0;
}
```
程序中的`sort`函数用于进行排序,使用了交换元素的辅助函数`swap`。在`main`函数中,首先要求用户输入三个数字,然后将它们的地址传递给`sort`函数进行排序,最后输出排序后的结果。
程序的思想是通过两两比较和交换元素的方式,将最小的元素移到第一位,然后在剩余的两个元素中寻找较小的元素放在第二位,最后剩下最大的元素放在第三位。这样就能实现将三个数字从小到大排列。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)