一、问题描述
题目描述:
给定一系列正整数,请按要求对数字进行分类,并输出以下 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。
输入样例 1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出样例 1:
30 11 2 9.7 9
输入样例 2:
8 1 2 4 5 6 7 9 16
输出样例 2:
N 11 2 N 9
二、AC代码
1.以数组记录各类数
2.对数组中的数进行相应的处理
3.某类数字不存在的话,要处理
#include<stdio.h>
int main(){
int a1[1001],a2[1001],a3[1001],a4[1001],a5[1001];
int cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0;
int num;
scanf("%d", &num);
int cnt=0;
while(cnt<num){
int temp;
scanf("%d", &temp);
cnt++;
if(temp%5==0&&temp%2==0){
a1[cnt1]=temp;
cnt1++;
}
else if(temp%5==1){
a2[cnt2]=temp;
cnt2++;
}
else if(temp%5==2){
a3[cnt3]=temp;
cnt3++;
}
else if(temp%5==3){
a4[cnt4]=temp;
//printf("\n%d ", a4[cnt4]);
cnt4++;
}
else if(temp%5==4){
a5[cnt5]=temp;
cnt5++;
}
}
int A1=0, A2=0, A3=0, A5=0;
double A4=0;
for(int i=0; i<cnt1; i++){
A1=A1+a1[i];
}
if(cnt1==0)
printf("N ");
else
printf("%d ", A1);
for(int i=0; i<cnt2; i++){
if(i%2==0)
A2=A2+a2[i];
else
A2=A2-a2[i];
}
if(cnt2==0)
printf("N ");
else
printf("%d ", A2);
A3=cnt3;
if(cnt3==0)
printf("N ");
else
printf("%d ", A3);
for(int i=0; i<cnt4; i++){
A4=A4+a4[i];
//printf("\na[%d]=%d ", i, a4[i]);
//printf("\n第%d个A4=%.1lf",i,A4);
}
//printf("\ncnt4=%d\n", cnt4);
A4=(double)A4/(double)cnt4;
if(cnt4==0)
printf("N ");
else
printf("%.1lf " ,A4);
for(int i=0; i<cnt5; i++){
if(A5<a5[i])
A5=a5[i];
}
if(cnt5==0)
printf("N");
else
printf("%d", A5);
}
三、总结
1.题目没什么难度,就是有点长
2.一定一定一定要注意读题啊!!!我就是因为题目读错了,导致以为第一个13和后面的数字一样也是一个输入,导致检查了千百遍,既没有语法错误也没有逻辑错误,但就是通过不了。切记读题啊(难受~)