zoukankan      html  css  js  c++  java
  • PAT 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

    遍历数组,然后按5种情况判断一下,分别计算一下,具体看代码。

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <math.h>
    #include <stdio.h>
    
    using namespace std;
    const int maxin=1000;
    char b;
    int a[maxin+5];//定义数组长度1005
    int A1,A2,A3,A4,A5;//分别代表五个答案
    int num;
    int main()
    {
        int cnt,cot=2,tot=0;
        while(scanf("%d",&cnt)!=EOF)
        {
            cot=2,tot=0;
            for(int i=0;i<cnt;i++)
                scanf("%d",&a[i]);
            A1=0;A2=0;A3=0;A4=0;A5=0;num=0;
            for(int i=0;i<cnt;i++)
            {
                if(a[i]%10==0) A1+=a[i];
                if(a[i]%5==1) {A2+=pow(-1.0,cot)*a[i];cot=(cot==1?2:1);num++;}
                if(a[i]%5==2) A3++;
                if(a[i]%5==3) A4+=a[i],tot++;
                if(a[i]%5==4) A5=max(A5,a[i]);
            }
            if(A1==0) printf("N ");else printf("%d ",A1);
            if(num==0) printf("N ");else printf("%d ",A2);
            if(A3==0) printf("N ");else printf("%d ",A3);
            if(A4==0) printf("N ");else printf("%.1f ",1.0*A4/tot);
            if(A5==0) printf("N
    ");else printf("%d
    ",A5);
        }
        return 0;
    
    }
  • 相关阅读:
    网摘习惯
    关于Application.DoEvents()
    五句话足以改变一生
    ActionForm中reset()的用法
    Java的MD5加密和解密类
    ibatis主键自动生成
    Parameter index out of range (3 > number of parameters
    ibatis 2.0 3.0 DTD
    SmartUpload在servlet中使用方法
    The prefix "tx" for element "tx:annotationdriven" is not bound.
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228784.html
Copyright © 2011-2022 走看看