供参考:
#include<stdio.h>
int main()
{
int n, cnt_e = 0, cnt_o = 0;
double sum_e = 0.0, sum_o = 0.0;
while (scanf("%d", &n) == 1 && n != 0)
{
if (n % 2 == 0)
{
cnt_e++;
sum_e += n;
}
else {
cnt_o++;
sum_o += n;
}
}
printf("Number of even:%d;Average of even:%.2f\n", cnt_e, sum_e / cnt_e);
printf("Number of odd:%d;Average of odd:%.2f\n", cnt_o, sum_o / cnt_o);
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <iomanip>
using namespace std;
int main()
{
int a[1005];
int n = 0;
int jcount = 0, ocount = 0, jsum = 0, osum = 0;
while(cin>>a[n] && a[n] != 0)
{
if ((a[n] & 1) == 0)
{
ocount++;
osum += a[n];
}
else
{
jcount++;
jsum += a[n];
}
n++;
}
cout<<"Number of even:"<<ocount<<"Average of even:"<<fixed<<setprecision(2)<<osum * 1.0 / ocount<<endl;
cout<<"Number of odd:"<<jcount<<"Average of odd:"<<fixed<<setprecision(2)<<jsum * 1.0 / jcount<<endl;
return 0;
}