1012 数字分类 (20 分)
代码如下:
#include<stdio.h>
int main()
{
int n,i,A1=0,A2=0,A3=0,A5=0,j=0,s=0;
double A4=0;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]%5==0&&a[i]%2==0)
A1+=a[i];
if(a[i]%5==1)
{
j++;
a[i]=j%2==0?-a[i]:a[i];
A2+=a[i];
}
if(a[i]%5==2)
A3++;
if(a[i]%5==3)
{
A4+=a[i];
s++;
}
if(a[i]%5==4&&A5<a[i])
A5=a[i];
}
if(A1)
printf("%d ",A1);
else printf("N ");
if(j)
printf("%d ",A2);
else printf("N ");
if(A3)
printf("%d ",A3);
else printf("%d ",A3);
if(s)
{
A4=A4*1.0/s;
printf("%.1lf ",A4);
}
else printf("N ");
if(A5)
printf("%d",A5);
else printf("N");
}
注:在筛选的时候可以把这些if语句改成开关语句,看起来可能会更简洁一点,可参考下面:
switch (number % 5)
{
case 0:
if (number % 2 == 0)
A1 += number; break;
case 1:
countA2++;
A2 += (countA2 % 2) ? number : (0 - number); break;
case 2:
A3++; break;
case 3:
countA4++;
A4 += number; break;
case 4:
if (number > A5)
A5 = number; break;
}