使用c++写一个程序:认真读入10个整数,使用冒泡法将它们降序排列后输出
时间: 2023-05-16 20:05:57 浏览: 55
以下是使用 C++ 写的程序,可以认真读入 10 个整数,使用冒泡法将它们降序排列后输出:
```cpp
#include <iostream>
using namespace std;
int main() {
int arr[10];
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]) {
int 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;
}
```
希望能对您有所帮助!
相关问题
用C++编写一个程序,用冒泡法将10个数按降序排列
好的,以下是用C++编写的冒泡排序降序排列的程序:
```c++
#include <iostream>
using namespace std;
int main() {
int arr[10] = {3, 7, 1, 8, 2, 5, 9, 4, 6, 10}; // 待排序的数组
int n = 10; // 数组长度
// 冒泡排序
for(int i=0; i<n-1; i++) {
for(int j=0; j<n-i-1; j++) {
if(arr[j] < arr[j+1]) {
// 交换arr[j]和arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// 输出排序后的数组
cout << "降序排列后的数组为:";
for(int i=0; i<n; i++) {
cout << arr[i] << " ";
}
return 0;
}
```
程序运行的结果为:
```
降序排列后的数组为:10 9 8 7 6 5 4 3 2 1
```
注意:这里的数组是手动给定的,实际使用时可以修改程序中的数组或者从用户处输入。
用C++代码写一个程序:任意读入一个年份,判断其是否为闰年
好的,以下是 C 代码:
```c
#include <stdio.h>
int main() {
int year;
printf("请输入一个年份:");
scanf("%d", &year);
if ((year % 4 == && year % 100 != ) || year % 400 == ) {
printf("%d 是闰年\n", year);
} else {
printf("%d 不是闰年\n", year);
}
return ;
}
```
以上代码可以读入一个年份,判断它是否为闰年。如果是闰年,输出“是闰年”,否则输出“不是闰年”。