当前位置:   article > 正文

PAT乙级题解——1012 数字分类 (20分)_每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 n 表示

每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 n 表示

给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:

输入格式:
每个输入包含 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 n, temp;
    int cnt[5] = {0};
    int ans[5] = {0};
    cin >> n;
    for (int i = 0; i < n; i++) {
        scanf("%d",&temp);
        if(temp % 5 == 0 && temp % 2 ==0){//A1
            cnt[0]++;
            ans[0] += temp;
        }else if (temp % 5 == 1){//A2
            if (cnt[1] % 2 != 1) {
                ans[1] += temp;
            }else{
                ans[1] -= temp;
            }
            cnt[1]++;
        }else if (temp % 5 == 2){//A3
            cnt[2]++;
        }else if (temp % 5 == 3){//A4
            cnt[3]++;
            ans[3] += temp;
        }else if (temp % 5 == 4 ){//A5
            if (temp > ans[4]) {
                ans[4] = temp;
            }
            cnt[4]++;
        }
    }
    for (int i = 0; i < 5; i++) {
        if (i != 0) {
            cout << " ";
        }
        if( cnt[i] != 0){
            switch (i) {
                case 0:cout << ans[i];
                    break;
                case 1:cout << ans[i];
                    break;
                case 2:cout << cnt[i];
                    break;
                case 3:printf("%.1f",(double)ans[i]/cnt[i]);
                    break;
                case 4:cout << ans[i];
                    break;
            }
        }else{
            cout << "N";
        }
    }
    return 0;
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/93670
推荐阅读
相关标签
  

闽ICP备14008679号