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
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7  
 8 
 9 int main(){
10     int count[5]={0};
11     int ans[5]={0};
12     int n,temp;
13     scanf("%d",&n);
14     for(int i=0;i<n;i++)
15     {
16         scanf("%d",&temp);
17         if(temp%5==0)
18         {
19             if(temp%2==0)
20             {
21                 count[0]++;
22                 ans[0]+=temp;
23             }
24         }else if(temp%5==1)
25         {
26             if(count[1]%2==0)
27             {
28             ans[1]+=temp;    
29             }else
30             {
31             ans[1]-=temp;    
32             }
33             count[1]++;
34         
35         }else if(temp%5==2)
36         {
37             count[2]++;
38         }else if(temp%5==3)
39         {
40             ans[3]+=temp;
41             count[3]++;
42         }else
43         {
44             if(ans[4]<temp)
45             {
46                 ans[4]=temp;
47                 count[4]++;
48             }
49         }
50     } 
51     if(count[0]==0)printf("N ");
52     else printf("%d ",ans[0]);
53     if(count[1]==0)printf("N ");
54     else printf("%d ",ans[1]);
55     if(count[2]==0)printf("N ");
56     else printf("%d ",count[2]);
57     if(count[3]==0)printf("N ");
58     else printf("%.1f ",ans[3]/(double)count[3]);
59     if(count[4]==0)printf("N");
60     else printf("%d",ans[4]);
61     
62     return 0;
63 }
原文地址:https://www.cnblogs.com/ligen/p/4298134.html