数字分类
给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:
A1 = 能被5整除的数字中所有偶数的和;
A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4…;
A3 = 被5除后余2的数字的个数;
A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
A5 = 被5除后余4的数字中最大数字。
#include <stdio.h>
#include <math.h>
int main()
{
int N,i,num[1000] = {};
int sum1 = 0,sum2 = 0,count3 = 0,count4 = 0,max = 0;
int next = 1;
float sum4 = 0;
scanf("%d",&N);
for(i = 0;i<N;i++)
{
scanf("%d",&num[i]);
}
for(i = 0;i<N;i++)
{
switch(num[i]%5)
{
case 0:
if(num[i]%2==0)
{
sum1 += num[i];
}
break;
case 1:
if(next == 1)
{
sum2 += num[i];
next = 0;
}
else if(next == 0)
{
sum2 -= num[i];
next = 1;
}
break;
case 2:
count3++;
break;
case 3:
count4++;
sum4 += num[i];
break;
case 4:
if(max<num[i])
{
max = num[i];
}
break;
}
}
if(sum1 == 0)
{
printf("N ");
}
else
{
printf("%d ",sum1);
}
if(sum2 == 0)
{
printf("N ");
}
else
{
printf("%d ",sum2);
}
if(count3 == 0)
{
printf("N ");
}
else
{
printf("%d ",count3);
}
if(sum4 == 0 || count4==0)
{
printf("N ");
}
else
{
printf("%.1f ",sum4/count4);
}
if(max == 0)
{
printf("N\n");
}
else
{
printf("%d\n",max);
}
return 0;
}
|