用c++ 题目要求编写一个程序,输入10个整数,统计并输出它们中非负数的个数,并计算输出它们的和。然后按照从小到大的顺序进行排列并输出,每行输出一个数据。
时间: 2024-05-07 11:21:13 浏览: 76
以下是一个简单的实现:
```c++
#include <iostream>
#include <algorithm>
int main() {
int nums[10];
int nonNegativeCount = 0;
int nonNegativeSum = 0;
// 输入10个整数
for (int i = 0; i < 10; i++) {
std::cin >> nums[i];
if (nums[i] >= 0) {
nonNegativeCount++;
nonNegativeSum += nums[i];
}
}
// 输出非负数的个数和它们的和
std::cout << "Non-negative count: " << nonNegativeCount << std::endl;
std::cout << "Non-negative sum: " << nonNegativeSum << std::endl;
// 排序并输出
std::sort(nums, nums + 10);
std::cout << "Sorted numbers: " << std::endl;
for (int i = 0; i < 10; i++) {
std::cout << nums[i] << std::endl;
}
return 0;
}
```
输入:
```
-1 2 3 -4 5 6 -7 8 9 0
```
输出:
```
Non-negative count: 6
Non-negative sum: 30
Sorted numbers:
-7
-4
-1
0
2
3
5
6
8
9
```
阅读全文