题目描述
给定一系列正整数,请按要求对数字进行分类,并输出以下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
8 1 2 4 5 6 7 9 16
样例输出
30 11 2 9.7 9
N 11 2 N 9
有个bug我想了半天
我掉的坑就是必须要有一个计数的数组,如果没有数组多用几个变量也行,因为得出来的A1,2,3,4,5可能为0,所以在输出的时候必须以计数的数组为标志而不是输出的数组为标志
#include<stdio.h>
int main(){
int temp;
int n;
int cout[5]={0};
int ans[5]={0};
int flag=0;
int flag2=0;
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
scanf("%d",&temp);
if(temp%5==0&&temp%2==0)
{
cout[0]=cout[0]+temp;
ans[0]++;
}//A1
else if(temp%5==1)
{
flag++;
if(flag%2==0)
{
cout[1]=cout[1]-temp;
}
else{
cout[1]=cout[1]+temp;
}
ans[1]++;
}//A2
else if(temp%5==2)
{
cout[2]++;
ans[2]++;
}//A3
else if(temp%5==3)
{
flag2++;
cout[3]=cout[3]+temp;
ans[3]++;
}//A4
else if(temp%5==4)
{
if(temp>cout[4])
{
cout[4]=temp;
ans[4]++;
}
}//A5
}
if(ans[0]==0){
printf("N ");
}
else printf("%d ",cout[0]);
if(ans[1]==0){
printf("N ");
}
else printf("%d ",cout[1]);
if(ans[2]==0){
printf("N ");
}
else printf("%d ",cout[2]);
if(ans[3]==0){
printf("N ");
}
else printf("%.1f ",(double)cout[3]/flag2);
if(ans[4]==0){
printf("N");
}
else printf("%d",cout[4]);
return 0;
}