题目:从键盘随机输入 10 个数,统计其中正数的个数及其平均数。
解题思路:用if逐一计算正数个数。
代码:
#include
using namespace std;
int main() {
int a, b, c, d, e, f, g, h, i, j, num=0, average;
cin >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j;
average = (a + b + c + d + e + f + g + h + i + j) / 10;
if (a > 0) num += 1;
if (b > 0) num += 1;
if (c > 0) num += 1;
if (d > 0) num += 1;
if (e > 0) num += 1;
if (f > 0) num += 1;
if (g > 0) num += 1;
if (h > 0) num += 1;
if (i > 0) num += 1;
if (j > 0) num += 1;
cout << “正数个数为” << num << endl << “平均数为” << average;
return 0;
}
06-08
3163