2007凯尔特人首场比赛:两道C语言数组的题目

来源:百度文库 编辑:高校问答 时间:2024/04/30 10:43:42
(1)a[10],求奇数,偶数的个数.
(2)a[10],求奇数的平均值.

include<stdio.h>
main()
{
int a[10],i,m=0,n=0,k=0;
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{
m++;
}
else
{
n++;
k+=a[i];
}
k=k/n;
}

两道合成一道:
#include "stdio.h"
#include "conio.h"
main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
int odd_num=0,even_num=0,total=0,i;
for(i=0;i<10;i++)
{
a[i]%2?odd_num++:even_num++;
total+=a[i];
}
printf("the odd number is %d.\nThe even number is %d.\nThe average is %g",odd_num,even_num,total/10.);
}

一楼的不错