编写一个c++程序,要求将输入的3个数排序
时间: 2023-03-11 18:55:12 浏览: 80
答:以下是一个将3个输入数字排序的C语言程序: #include <stdio.h> int main()
{
int num1, num2, num3, temp;
printf("请输入三个数:");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 > num3)
{
temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 > num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
printf("排序后的数为:%d %d %d", num1, num2, num3);
return 0;
}
相关问题
帮我编写一个c++代码,要求能够对输入的数字大小进行排序
好的,以下是一个简单的 C++ 程序,使用冒泡排序算法对输入的数字进行排序:
```cpp
#include <iostream>
using namespace std;
int main() {
int n, i, j, temp;
cout << "请输入要排序的数字个数:";
cin >> n;
int arr[n];
cout << "请输入" << n << "个数字:";
for (i = 0; i < n; i++) {
cin >> arr[i];
}
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;
}
}
}
cout << "排序后的数字为:";
for (i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
你可以将以上代码复制到你的 C++ 编辑器中,然后运行程序即可。程序会提示你输入要排序的数字个数和具体的数字,然后输出排序后的结果。
用C++iostream编写一个边输入数字边对数字进行插入排序的程序
好的,以下是使用C++标准库中的iostream库编写的边输入数字边进行插入排序的程序:
```c++
#include <iostream>
using namespace std;
int main() {
int n, i, j, temp;
cout << "Enter the number of elements you want to sort: ";
cin >> n;
int arr[n];
cout << "Enter the elements:" << endl;
for (i = 0; i < n; i++) {
cin >> arr[i];
// Insertion Sort
for (j = i - 1; j >= 0; j--) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
else {
break;
}
}
// Print the sorted array
cout << "Sorted Array: ";
for (j = 0; j <= i; j++) {
cout << arr[j] << " ";
}
cout << endl;
}
return 0;
}
```
这个程序与前面的C语言版本的程序类似,使用了C++标准库中的iostream库。在输入和输出时使用了cout和cin。