用c++编写程序,输入10个整数数据,存放到数组中, 并将其降序输出
以下是使用冒泡排序法实现的代码:
#include <iostream>
using namespace std;
int main() {
int arr[10], temp;
cout << "请输入10个整数:" << endl;
for (int i = 0; i < 10; i++) {
cin >> arr[i];
}
// 冒泡排序
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9 - i; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 输出降序结果
cout << "降序排列后的结果为:" << endl;
for (int i = 0; i < 10; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
示例输入:
5 4 8 1 9 2 7 3 6 0
示例输出:
请输入10个整数:
5 4 8 1 9 2 7 3 6 0
降序排列后的结果为:
9 8 7 6 5 4 3 2 1 0
原文地址: https://www.cveoy.top/t/topic/vk2 著作权归作者所有。请勿转载和采集!