PAT乙级1012

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805311146147840

题解

就比较简单,判断每个数字是哪种情况,然后进行相应的计算即可。

下面的代码中其实数组是不必要的,每取一个数字就可以直接进行相应计算。

// PAT BasicLevel 1012
// https://pintia.cn/problem-sets/994805260223102976/problems/994805311146147840

#include <iostream>
using namespace std;

int main()
{
    // 数字个数
    int n;
    cin >> n;

    // 获取数字
    int* numArr=new int[n];
    for(int i=0;i<n;++i){
        cin >> numArr[i];
    }

    // 遍历数组,计算A1至A5
    int a1=0,a1Count=0;
    int a2=0,flag=1,a2Count=0;
    int a3=0,a3Count=0;
    double a4Sum=0,a4Count=0;
    int a5=0,a5Count=0;
    for(int i=0;i<n;++i){
        switch(numArr[i]%5){
        case 0:
            if(numArr[i]%2==0){
                a1+=numArr[i];
                a1Count++;
            }
            break;
        case 1:
            a2+=flag*numArr[i];
            flag=-flag;
            a2Count++;
            break;
        case 2:
            a3++;
            a3Count++;
            break;
        case 3:
            a4Sum+=numArr[i];
            a4Count++;
            break;
        case 4:
            if(numArr[i]>a5){
                a5=numArr[i];
                a5Count++;
            }
            break;
        }
    }

    // 输出A1至A5
    if(a1Count>0){
        cout << a1 << ' ';
    }else{
        cout <<"N ";
    }

    if(a2Count>0){
        cout << a2 << ' ';
    }else{
        cout << "N ";
    }

    if(a3Count>0){
        cout << a3 << ' ';
    }else{
        cout << "N ";
    }

    if(a4Count>0){
        printf("%.1lf ", a4Sum / a4Count);
    }else{
        cout << "N ";
    }

    if (a5Count > 0){
        cout << a5;
    }
    else{
        cout << 'N';
    }

    delete[] numArr;
    //system("pause");
    return 0;
}

作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!


posted @ 2019-08-04 23:47  臭咸鱼  阅读(192)  评论(0编辑  收藏  举报