c语言写个程序,将三个数字从小到大排列
时间: 2023-02-25 07:11:01 浏览: 197
下面是一个使用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语句进行交换实现了从小到大排列。
相关问题
用c语言从小到大排列任意三个整数
我可以回答这个问题。以下是用 C 语言从小到大排列任意三个整数的代码:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d%d%d", &a, &b, &c);
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
printf("从小到大排列的结果是:%d %d %d\n", a, b, c);
return 0;
}
```
你可以将三个整数输入到程序中,程序会自动将它们从小到大排列,并输出结果。
用c语言表达把任意三个实数从小到大排列
这是一个排序算法的问题,可以使用冒泡排序、快速排序等方法来实现,以下是冒泡排序的示例代码:
#include <stdio.h>
void bubble_sort(float arr[], int n) {
int i, j;
float temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
float arr[3];
int i;
printf("Enter three real numbers: ");
for (i = 0; i < 3; i++) {
scanf("%f", &arr[i]);
}
bubble_sort(arr, 3);
printf("Sorted numbers: %.2f, %.2f, %.2f", arr[0], arr[1], arr[2]);
return 0;
}