PAT (Basic Level) Practice (中文)1012 数字分类

时间:2022-07-24
本文章向大家介绍PAT (Basic Level) Practice (中文)1012 数字分类,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1012 数字分类

给定一系列正整数,请按要求对数字进行分类,并输出以下 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

代码:

#include<stdio.h>
int main()
{

        int N;
        int arr[1000001];
        scanf("%d",&N);
        int i;
        for(i=0;i<N;i++)
        {
            scanf("%d",&arr[i]);
        }
        int A1,A2,A3,A4,A5;
        int arr5[1000001];
        int t4=0;
        int temp1,temp2,temp3,temp4,temp5;
        temp1=temp2=temp3=temp4=temp5=-1;
        A1=0;
        A2=0;
        A3=0;
        A4=0;
        A5=0;
        int l=1;
        for(i=0;i<N;i++)
        {
            if(arr[i]%5==0&&arr[i]%2==0) {temp1=1;A1+=arr[i];}
            if(arr[i]%5==1){temp2=1;A2+=(arr[i]*l);l=-l;}
            if(arr[i]%5==2) {temp3=1; A3++;}
            if(arr[i]%5==3) {temp4=1; A4+=arr[i];t4++;}
            if(arr[i]%5==4) {temp5=1; arr5[A5++]=arr[i];}
        }
        //printf("a4==%d t4==%lfn",A4,t4);
        if(temp1==1) printf("%d ",A1);
        else printf("N ");
        if(temp2==1) printf("%d ",A2);
        else printf("N ");
        if(temp3==1) printf("%d ",A3);
        else printf("N ");
        if(temp4==1) printf("%.1lf ",A4/(double)t4);
        else printf("N ");
        if(temp5==1)
        {
            int index=0;
            for(i=1;i<A5;i++)
            {
                if(arr5[i]>=arr[index]) index=i;
            }
            printf("%dn",arr5[index]);
        }
        else printf("Nn");
        return 0;

}