给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:
A1 = 能被5整除的数字中所有偶数的和;
A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4…;
A3 = 被5除后余2的数字的个数;
A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
A5 = 被5除后余4的数字中最大数字。
输入描述:
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。
输出描述:
对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。
若其中某一类数字不存在,则在相应位置输出“N”。
输入例子:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出例子:
30 11 2 9.7 9
#include<stdio.h>
#define MAX 1000
int main()
{
int N,n;
int ans1=0;
int A2[MAX],i,count2=0,ans2=0,minus;
int count3=0;
int ans4=0,count4=0;
int max=0;
int flag[5]={0};
scanf("%d",&N);
while(N--)
{
scanf("%d",&n);
//make decision
switch(n%5)
{
case 0:
if(n%2==0)
{
ans1+=n;
flag[0]=1;
}
break;
case 1:
//count2-1 is the last element in A2
A2[count2++]=n;
flag[1]=1;
break;
case 2:
count3++;
flag[2]=1;
break;
case 3:
ans4+=n;
count4++;
flag[3]=1;
break;
case 4:
if(n>max)
{
max=n;
flag[4]=1;
}
break;
}
}
//output ans
if(!flag[0])
printf("N ");
else
printf("%d ",ans1);
if(!flag[1])
printf("N ");
else
{
minus=-1;
for(i=0;i<count2;i++)
{
minus=-minus;
ans2+=minus*A2[i];
}
printf("%d ",ans2);
}
if(!flag[2])
printf("N ");
else
printf("%d ",count3);
if(!flag[3])
printf("N ");
else
printf("%.1f ",(float)ans4/count4);
if(!flag[4])
printf("N\n");
else
printf("%d\n",max);
return 0;
}