码迷,mamicode.com
首页 > 其他好文 > 详细

1012. 数字分类 (20)

时间:2017-03-19 15:29:31      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:iostream   ++   use   ble   main   用例   nbsp   sys   return   

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
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstdio>  

using namespace std;

int main()
{
    int n;
    cin >> n;
    int cnt[5] = {0}, rst[5] = { 0 };
    int flag = 1;
    for (int i = 0; i < n; i++)
    {
        int temp;
        cin >> temp;
        if (temp % 5 == 0)
        {
            if (temp % 2 == 0)
            {
                cnt[0]++;
                rst[0] += temp;
            }
        }
        else if (temp % 5 == 1)
        {
            cnt[1]++;
            rst[1] += flag*temp;
            flag = -flag;
        }
        else if (temp % 5 == 2)
        {
            cnt[2]++;
        }
        else if (temp % 5 == 3)
        {
            cnt[3]++;
            rst[3] += temp;
        }
        else if (temp % 5 == 4)
        {
            cnt[4]++;
            if (temp > rst[4]) rst[4] = temp;
        }
        
    }

    if (cnt[0] == 0) cout << "N ";
    else  cout << rst[0] << " ";
    if (cnt[1] == 0) cout << "N ";
    else  cout << rst[1] << " ";
    if (cnt[2] == 0) cout << "N ";
    else  cout << cnt[2] << " ";
    if (cnt[3] == 0) cout << "N ";
    else  printf("%.1f ", (double)rst[3] / cnt[3]);
    if (cnt[4] == 0) cout << "N";
    else  cout << rst[4] ;

    system("pause");
    return 0;
}

 

1012. 数字分类 (20)

标签:iostream   ++   use   ble   main   用例   nbsp   sys   return   

原文地址:http://www.cnblogs.com/brightz2017/p/6580474.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!