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/

欢迎讨论和交流!


转载于:https://www.cnblogs.com/chouxianyu/p/11300602.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值