ZUT源程序评判系统_分项平均值

给出一个整数的一维数组,请分别计算该数组中奇数项(数组下标为奇数)的平均值和偶数项(数组下标为偶数,包括下标为0的元素)的平均值。(注意,这里数组元素、平均值都采用整数类型,数组下标从0开始。)
输入数据只有一组,包括两行。第一行是一个正整数n(2<=n<=200),表示数组有多少个元素;第二行则输入这n个数组元素。
在一行上输出两个值,即偶数项的平均值和奇数项的平均值。
代码如下:
#include <stdio.h>
#define SIZE 200
int main()
{
 int  a[SIZE],i;
 int n,a1,a2,s1=0,s2=0;
 scanf("%d", &n);
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 
 for (i = 0; i < n; i++)
 {
  if (i % 2 != 0)
   s1 =s1+ a[i];
  else
   s2 =s1+ a[i];
 }
 if (n % 2 == 0)
 {
  a1 = s1 / (n / 2);
  a2 = s2 / (n / 2);
 }
 else
 {
  a1 = s1 / ((n-1) / 2);
  a2 = s2 / ((n +1)/ 2 );
 }
 printf("%d %d", a1, a2);
 return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43759910/article/details/84794245