求出一批非零整数中的奇数、偶数的平均值,用0作为终止标记。要求按奇数平均值,偶数平均值的形式输出结果。输出数据不控制列宽,均保留2位小数。例如输入 3 6 8 3 1 7 0 则应输出 3.50,7.00 注意:语言选DEV C++。
时间: 2024-05-15 18:15:11 浏览: 134
c++ 求一些数的平均值。
```c
#include <stdio.h>
int main() {
int num, sum_odd = 0, count_odd = 0, sum_even = 0, count_even = 0;
while (scanf("%d", &num) && num != 0) {
if (num % 2 == 0) {
sum_even += num;
count_even++;
} else {
sum_odd += num;
count_odd++;
}
}
if (count_odd == 0) { // 防止除数为0
printf("0.00,");
} else {
printf("%.2f,", (float) sum_odd / count_odd);
}
if (count_even == 0) {
printf("0.00");
} else {
printf("%.2f", (float) sum_even / count_even);
}
return 0;
}
```
阅读全文