PAT Basic 1012 数字分类 (20 分)

给定一系列正整数,请按要求对数字进行分类,并输出以下 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 <iostream>
using namespace std;
int main()
{
    int T,tmp,sign=1;
    int ans1=0,ans2=0,ans3=0,ans5=0;double ans4=0,coun=0;
    bool flag1=false,flag2=false,flag3=false,flag4=false,flag5=false;
    cin>>T;
    while(T--){
        cin>>tmp;
        if(tmp%10==0) {
            ans1+=tmp;
            flag1=true;
        }
        if(tmp%5==1) {
            ans2+=(tmp*sign);
            sign=-sign;
            flag2=true;
        }
        if(tmp%5==2) {
            ans3++;
            flag3=true;
        }
        if(tmp%5==3) {
            ans4+=tmp;
            coun++;
            flag4=true;
        }
        if(tmp%5==4) {
            if(tmp>ans5) ans5=tmp;
            flag5=true;
        }
    }
    ans4/=coun;
    if(!flag1) cout<<"N "; else cout<<ans1<<" ";
    if(!flag2) cout<<"N "; else cout<<ans2<<" ";
    if(!flag3) cout<<"N "; else cout<<ans3<<" ";
    if(!flag4) cout<<"N "; else printf("%.1f ",ans4);
    if(!flag5) cout<<"N"; else cout<<ans5;
    system("pause");
    return 0;
}

 

posted @ 2019-08-23 01:02  SteveYu  阅读(207)  评论(0编辑  收藏  举报