c++语言三个数从小到大排序
时间: 2023-10-30 15:07:01 浏览: 145
C++代码如下:
```
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if (a > b) swap(a, b);
if (a > c) swap(a, c);
if (b > c) swap(b, c);
cout << a << " " << b << " " << c << endl;
return 0;
}
```
相关问题
用c++写代码:三个数从小到大排列
可以使用以下代码实现:
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "请输入三个数,以空格分隔:" << endl;
cin >> a >> b >> c;
if (a > b) swap(a, b);
if (a > c) swap(a, c);
if (b > c) swap(b, c);
cout << "从小到大排列的结果为:" << a << " " << b << " " << c << endl;
return 0;
}
C++ 写三个数据从小到大输出
可以使用冒泡排序算法来实现:
```c
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j, 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() {
int arr[3];
int i;
printf("请输入三个整数:\n");
for (i = 0; i < 3; i++) {
scanf("%d", &arr[i]);
}
bubble_sort(arr, 3);
printf("从小到大排序后的结果为:\n");
for (i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
这段代码可以让用户输入三个整数,然后使用冒泡排序算法将它们从小到大排序,最后输出排序后的结果。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)