算法题:数字分类

题目描写叙述

给定一系列正整数,请按要求对数字进行分类,并输出下面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”。

输入样例:

13 1 2 3 4 5 6 7 8 9 10 20 16 18

输出样例:

30 11 2 9.7 9

#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int num;

    int countA = 0;
    int countB = 0;
    int countC = 0;
    int countD = 0;
    int numD = 0;
    int maxCount = 0xffffffff;

    int flags = 1;
    int m = n;
    while (n--)
    {
        cin >> num;
        if (num % 10 == 0)
        {
            countA += num;
        }
        if (num % 5 == 1)
        {
            if (flags == 1)
            {
                countB += num*flags;
                flags = -1;
            }
            else
            {
                countB += num*flags;
                flags = 1;
            }
        }
        if (num % 5 == 2)
        {
            countC++;
        }
        if (num % 5 == 3)
        {
            countD += num;
            numD++;
        }
        if (num % 5 == 4)
        {
            if (maxCount < num)
            {
                maxCount = num;
            }
        }
    }
    if (countA == 0)
    {
        cout << "N" << " ";
    }
    else
    {
        cout << countA << " ";
    }
    if (countB == 0)
    {
        cout << "N" << " ";
    }
    else
        cout << countB << " ";

    if (countC == 0)
    {
        cout << "N" << " ";
    }
    else
        cout << countC << " ";
    if (countD == 0)
    {
        cout << "N" << " ";
    }
    else
        cout << setiosflags(ios::fixed) << setprecision(1) << (double)countD / numD << " ";
    if (maxCount == 0xffffffff)
    {
        cout << "N" <<endl;
    }
    else
    {
        cout << maxCount << endl;
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liguangsunls/p/7249229.html