输入三个数进行大小排列,c++代码
时间: 2023-04-04 08:03:56 浏览: 156
好的,以下是 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++比较三个数大小并按大到小输出
可以使用冒泡排序的思想,先比较两个数的大小,将较大的数放在前面,再用同样的方法比较另外两个数,最后得到三个数的大小关系,按大到小输出即可。
以下是一种可能的实现方式:
```c++
#include <iostream>
using namespace std;
int main() {
int a, b, c, temp;
cout << "请输入三个整数:";
cin >> a >> b >> c;
// 比较a和b
if (a < b) {
temp = a;
a = b;
b = temp;
}
// 比较a和c
if (a < c) {
temp = a;
a = c;
c = temp;
}
// 再比较b和c
if (b < c) {
temp = b;
b = c;
c = temp;
}
// 输出结果
cout << a << " " << b << " " << c << endl;
return 0;
}
```
注意,上述代码中使用了一个temp变量来交换两个数的值,也可以使用C++11中新增的std::swap函数来实现交换。
c++用三个函数实现数组的输入排序输出
下面是使用三个函数实现数组输入排序输出的C++代码示例:
```c++
#include <iostream>
using namespace std;
// 输入数组
void inputArray(int arr[], int size) {
cout << "请输入" << size << "个整数:" << endl;
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
}
// 冒泡排序
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 输出数组
void outputArray(int arr[], int size) {
cout << "排序后的数组为:" << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int size;
cout << "请输入数组大小:";
cin >> size;
int arr[size];
inputArray(arr, size);
bubbleSort(arr, size);
outputArray(arr, size);
return 0;
}
```
这个程序首先要求用户输入数组大小,然后使用 `inputArray` 函数输入数组元素。接下来,使用 `bubbleSort` 函数对数组进行冒泡排序。最后,使用 `outputArray` 函数输出排序后的数组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)