1012 数字分类

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

结果统一存放在result数组中, 同时用count数组来记录有无满足某条件的值, 若count[i]=0,则说明没有结果, 应输出"N"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

int main()
{
	int i = 0, j = 0, n, a[1010];
	double sum3 = 0;
	double count[5] = {0}, result[5] = {0};
	
	cin >> n;
	for(int i = 0; i < n; ++ i)
	{
		cin >> a[i];
	} 
	
	for(i = 0; i < n; ++ i)
	{
		if(a[i] % 10 == 0)
		{
			result[0] += a[i];
			count[0] ++;
		}
		else if(a[i] % 5 == 1)
		{
			result[1] += pow(-1, j++) * a[i];
			count[1] ++;
		}
		else if(a[i] % 5 == 2)
		{
			count[2] ++;
			result[2] ++;
		}
		else if(a[i] % 5 == 3)
		{
			sum3 += a[i];
			count[3] ++;
		}
		else if(a[i] % 5 == 4)
		{
			result[4] = max((int)result[4], a[i]);
			count[4] ++;
		}
	}
	
	result[3] = sum3 / count[3];
	
	for(int i = 0; i < 3; ++ i)
	{
		if(count[i] == 0)
		{
			printf("N ");
		}
		else
		{
			printf("%.f ", result[i]);
		}
	}
	
	if(count[3] == 0)
	{
		printf("N ");
	}
	else
	{
		printf("%.1f ", result[3]);
	}
	
	if(count[4] == 0)
	{
		printf("N");
	}
	else
	{
		printf("%.f", result[4]);
	}
	
	return 0;
}

  

posted @ 2019-05-10 19:31  青衫客36  阅读(134)  评论(0编辑  收藏  举报